将uiimage设置为nil不会使用ARC释放内存 [英] Setting uiimage to nil doesn't release memory with ARC

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

问题描述

我有一个滚动视图,它在滚动页面时显示不同的图像,例如PhotoScroller.我正在使用ARC.当有人滚动到另一个页面时,我将UIImageView的当前未显示的图像属性设置为nil,这是(尝试)以避免内存崩溃(这种情况仍在发生).然后,当用户滚动到新页面时,该页面的图像以及该页面之前和之后的页面均设置为UIImageView的image属性(以平滑查看).页面的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;

- (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释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆