开发者

crash when loading big sizes images on scroll view iPad

I have a 31 images with sizes 1448 *2048 . I have to add it on scroll view and swap horizontally 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];
    i开发者_StackOverflow中文版f ((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 function 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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜