tags:

views:

77

answers:

2

Hi Folks ,

I have a 31 images with sizes 1448 *2048 . I have to add it on scroll view and swap horizontly on iPad. But problem is when i swap across 6th images . It crashes with memory warning .

I have used the logic of page control example from developer.apple.com. in ViewDidload of this class I have set the scroll view frame same as example of page control.

My page load code function is

- (void)loadPage:(int)page 
{

 if (page < 0) return;
    if (page >= [_imgArray count]) return;

    // replace the placeholder if necessary aViewController is NSMutable Array.
    ImageViewC *controller = [aViewControllers objectAtIndex:page];
    if ((NSNull *)controller == [NSNull null]) {

        controller = [[ImageViewC alloc] initWithImage:[_imgArray objectAtIndex:page]];
  //controller.screen = currentPage;
  [aViewControllers replaceObjectAtIndex:page withObject:controller];
        [controller release];
    }

    // add the controller's view to the scroll view
    if (nil == controller.view.superview)
 {
        CGRect frame = scrollView.frame;
        frame.origin.x = frame.size.width * page;
        frame.origin.y = 0;
        controller.view.frame = frame;
        [scrollView addSubview:controller.view];
    }
}

And scroll funtion is

- (void)scrollViewDidScroll:(UIScrollView *)sender
{

    // Switch the indicator when more than 50% of the previous/next page is visible
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;

    self.currentPage = floor(scrollView.contentOffset.x / pageWidth) + 1;
    // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
    [self loadPage:page - 1];
    [self loadPage:page];
    [self loadPage:page + 1];

 if (page == -1) 
  return;
}

Can any one help me. And Please modify the code how to manage the memory so that i would be able to swap 31 images on this scroll.

Thanks in Advance

+1  A: 

One image takes up more than 11 MB of RAM, so you shouldn't be surprised to see your app crashing.

You need to release the images that are not visible, and you should maybe partitionize your images as well. The loadPage: method looks overly complicated and I can't get what it's doing, and I don't know what ImageViewC is.

If you are a registered developer at Apple, check out the WWDC videos, one of them is about UIScrollViews and it shows how to load many images and get the handling right.

Eiko
The WWDC example in question is "photoscroller" from 2010; the code is available as well as the video. Both are excellent!
JosephH
Thanks. In this code imageViewC is controller class in which there is imageView. Can u send the link for wwdc vedeos for uiscrollviews.
Riyaz Ahemad
The videos are available from Apple's website after login - it will take you to iTunes.
Eiko
A: 

You should unload images that are not currently visible. This should help keep the memory footprint down.

Jeff