将 uiimage 设置为 nil 不会使用 ARC 释放内存

Setting uiimage to nil doesn#39;t release memory with ARC(将 uiimage 设置为 nil 不会使用 ARC 释放内存)

本文介绍了将 uiimage 设置为 nil 不会使用 ARC 释放内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个滚动视图,可以在滚动页面时显示不同的图像,例如 PhotoScroller.我正在使用ARC.当有人滚动到另一个页面时,我将当前未显示的 UIImageView 的图像属性设置为 nil,因为(试图)避免内存崩溃,这种情况仍在发生.然后当用户滚动到一个新页面时,该页面的图像被设置为 UIImageView 的图像属性,以及它之前和之后的页面(为了流畅查看).页面的 UIImage 都保存在一个数组中.然而,当我滚动页面时,内存使用量一直在上升,好像将 UIImageView 的 image 属性设置为 nil 并没有从内存中释放它.我使用 initWithContentsOfFile 来初始化我的 UIImages.我也尝试使用 imageNamedimageWithContentsOfFile ,但没有成功.这是我的滚动视图代码:

I have a scrollview that shows different images as it's scrolled through the pages, like PhotoScroller. I'm using ARC. When someone scrolls to another page, I set the image property of the UIImageView not being currently show to nil, as (attempting) to avoid memory crashes, which are still happening. Then when the user scrolls to a new page, the image for that page is set as the UIImageView's image property, as well as the page before and after it (for smooth viewing). The UIImage's for the pages are all held in an array. Yet as I scroll through the pages, memory usage keeps going up, as if setting the UIImageView's image property to nil isn't releasing it from memory. I use initWithContentsOfFile to initialize my UIImages. I tried with imageNamed and imageWithContentsOfFile too, with no luck. Here's my scrollview code:

<代码>- (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`

推荐答案

页面的 UIImage 都保存在一个数组中.

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

当您将 UIImageView 的属性设置为 nil 时,UIImage 不会被释放,因为数组仍然持有一个引用给他们.至于内存增长,可能是正在分配的其他东西.我建议查看 Instrument 的对象分配工具,以追踪滚动时到底有什么增长.

The UIImage's are not being deallocated when you set the UIImageView's property to nil because the array is still holding a reference to them. As for the memory growth, it may be something else that is being allocated. I'd suggest taking a look with Instrument's object allocation instrument to track down what exactly is growing as you scroll.

这篇关于将 uiimage 设置为 nil 不会使用 ARC 释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将 uiimage 设置为 nil 不会使用 ARC 释放内存

基础教程推荐