无法在ARC上释放内存泄漏iOS(UIImageView,UIImage,CGImage) [英] Memory Leak iOS (UIImageView,UIImage,CGImage) not freed on ARC

查看:370
本文介绍了无法在ARC上释放内存泄漏iOS(UIImageView,UIImage,CGImage)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个简单的视频流,但是由于某种原因,我的内存无法释放:

Im trying to implement a simple video stream but for some reason my memory won't get freed:

(void)updateImage:(UIImage *)image{
  self.indicator.hidden = TRUE;
  //CGImageRelease([self.imageView.image CGImage]);
  self.imageView.image = nil;
  self.imageView.image = image;
  [self.imageView setNeedsDisplay];
}

如果我使用

CGImageRelease([self.imageView.image CGImage]);

内存将被释放.但是,当我返回以前的视图控制器时,该应用程序将崩溃,因为它尝试释放该图像的已分配内存,而该内存已经释放了.从异步任务中调用此方法,该任务使用以下方法创建图像:

memory will be freed. But when I return to a previous view controller the app will crash as it tries to free the allocated memory for that image, which I already freed. This method is called from a async task which creates an image using:

UIImage *image = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
CGDataProviderRelease(provider);
CFRelease(data);

据我了解,UIImage现在拥有CGImage,我不必释放它. 那么,有什么方法可以确保当我用新图像更新UIImageView时释放UIImage吗?

As I understood it the UIImage now owns the CGImage and I shouldn't have to release it. So is there anyway to ensure that the UIImage is freed when I updated the UIImageView with a new image?

推荐答案

好的,所以我终于找到了问题所在.

Ok so I finally figured out the problem.

正如我所说,我正在使用一些后台线程来执行图像更新,解决方案是将其添加为自动释放池,如下所示:

As I said I was using some background thread to perform the image update, the solution was to add it as a autorelease pool as following:

   @autoreleasepool {
        [self.delegate performSelectorOnMainThread:@selector(updateImage:) withObject:[self fetchImage] waitUntilDone:NO];
    }

这篇关于无法在ARC上释放内存泄漏iOS(UIImageView,UIImage,CGImage)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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