懒人图像加载&卸载内存分配问题.... Aaaargghhh

时间:2010-09-07 17:48:38

标签: iphone uiimage memory-management

我正在构建一个滚动浏览100张房屋图像的滚动视图。 有用。但是......对于每个查看的图像,分配的内存增加了2.5 MB。最终,应用程序因内存不足而崩溃。

即使我使用下面代码中指定的相同图像,由于内存分配,我内存不足。

我认为当用另一个对象替换时,NSMutableArray会释放我的图像(因为保留计数变为零)。

什么让我的图像不被卸载?如何卸载图像?

CodeFragment:

/*!
    @method     
    @abstract   Loads & places an house image.
    @discussion Loads & places an house image and places it on the page at the proper location.
*/
- (void)loadScrollViewWithPage:(int)page {
    if (page < 0) return;
    if (page >= [fetchedObjects count]) return;

    // replace the placeholder if necessary
    [self setSelectedMiniature:[fetchedObjects objectAtIndex:page]];
    NSString *imageLocation = [selectedMiniature image];

    houseImageView = [imageViewArray objectAtIndex:page];
    if ((NSNull *)houseImageView == [NSNull null]) {
        // Use imageWithContentsOfFile: bacause imageNamed: is cached.
        UIImage *houseImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"miniature91.jpg", imageLocation] ofType:nil]];

        houseImageView = [[UIImageView alloc] initWithImage:houseImage];
        [imageViewArray replaceObjectAtIndex:page withObject:houseImageView];
        [houseImageView release];
    }

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

}

- (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;

    // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
    [self clearPage:page - 2];  
    [self loadScrollViewWithPage:page - 1];
    [self loadScrollViewWithPage:page];
    [self loadScrollViewWithPage:page + 1];
    [self clearPage:page + 2];  

    // A possible optimization would be to unload the views+controllers which are no longer visible 
//  for (int i = 0; i < [imageArray count]; i++) {   
//      if (i < (page - 1) || i > (page + 1)) {                   
//          [imageArray replaceObjectAtIndex:i withObject:[NSNull null]];               
//      } 
//  } 

}

- (void)clearPage:(int)page {
    if (page < 0) return;
    if (page >= [fetchedObjects count]) return;
    [imageViewArray replaceObjectAtIndex:page withObject:[NSNull null]];                 
}

// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self swapSliderForRatingView];
    //[self showInfo:YES];
}

// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self swapSliderForRatingView];
    [self updateMiniatureInfo];
    //[self showInfo:NO];
}

1 个答案:

答案 0 :(得分:1)

在快速查看代码之后,我可以看到您正在管理一组图像视图,并可能在适当的时候删除它们,但我没有发现您从滚动视图中删除图像视图的位置'重新进入。

只要这些视图是子视图列表的一部分,它们就不会被释放。这可能就是你遇到内存麻烦的原因。