Setting uiimage to nil doesn#39;t release memory with ARC(将 uiimage 设置为 nil 不会使用 ARC 释放内存)
问题描述
我有一个滚动视图,可以在滚动页面时显示不同的图像,例如 PhotoScroller.我正在使用ARC.当有人滚动到另一个页面时,我将当前未显示的 UIImageView 的图像属性设置为 nil,因为(试图)避免内存崩溃,这种情况仍在发生.然后当用户滚动到一个新页面时,该页面的图像被设置为 UIImageView 的图像属性,以及它之前和之后的页面(为了流畅查看).页面的 UIImage 都保存在一个数组中.然而,当我滚动页面时,内存使用量一直在上升,好像将 UIImageView 的 image 属性设置为 nil 并没有从内存中释放它.我使用
initWithContentsOfFile
来初始化我的 UIImages.我也尝试使用 imageNamed
和 imageWithContentsOfFile
,但没有成功.这是我的滚动视图代码:
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 释放内存
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01