ios – 将uiimage设置为nil不会释放ARC内存

我有一个滚动视图,显示不同的图像,因为它滚动浏览页面,如PhotoScroller.我正在使用ARC.当有人滚动到另一个页面时,我将当前未显示的UI
ImageView的image属性设置为nil,因为(尝试)避免仍在发生的内存崩溃.然后,当用户滚动到新页面时,该页面的图像被设置为UIImageView的图像属性,以及它之前和之后的页面(用于平滑查看).页面的UIImage都保存在一个数组中.然而,当我滚动页面时,内存使用量不断上升,就好像将UIImageView的image属性设置为nil不会从内存中释放它一样.我使用initWithContentsOfFile来初始化我的UIImages.我也试过了imageNamed和imageWithContentsOfFile,没有运气.这是我的scrollview代码:

– (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int indexShown = self.scrollView.bounds.origin.x / kScrollObjWidth;

for(NSNumber *index in indexesToRemove)
{
    UIImageView *imgViewToRemove = [[self.scrollView subviews] objectAtIndex:[index intValue]];
    imgViewToRemove.image = nil;
}
[indexesToRemove removeAllObjects];

UIImageView *imgViewToReplace = [[self.scrollView subviews] objectAtIndex:indexShown];
[imgViewToReplace setImage:[pageUIImagesArr objectAtIndex:indexShown]];
[indexesToRemove addObject:[NSNumber numberWithInt:indexShown]];

if(indexShown != 0 && ![[[self.scrollView subviews] objectAtIndex:indexShown-1] image])
{
    imgViewToReplace = [[self.scrollView subviews] objectAtIndex:indexShown-1];
    [imgViewToReplace setImage:[pageUIImagesArr objectAtIndex:indexShown-1]];
    [indexesToRemove addObject:[NSNumber numberWithInt:indexShown-1]];
}
if(indexShown != kNumImages-1 && ![[[self.scrollView subviews] objectAtIndex:indexShown+1] image])
{
    imgViewToReplace = [[self.scrollView subviews] objectAtIndex:indexShown+1];
    [imgViewToReplace setImage:[pageUIImagesArr objectAtIndex:indexShown+1]];
    [indexesToRemove addObject:[NSNumber numberWithInt:indexShown+1]];
}

currentView = [[self.scrollView subviews] objectAtIndex:indexShown];
//check which view is being shown`

最佳答案

The UIImage’s for the pages are all held in an array.

当您将UIImageView的属性设置为nil时,UIImage不会被释放,因为该数组仍然保持对它们的引用.至于内存增长,它可能是正在分配的其他东西.我建议看一下Instrument的对象分配工具,以便在滚动时跟踪确切增长的内容.

点赞