imageWithContentsOfFile 内存问题

imageWithContentsOfFile memory issue

我的项目具有图像共享功能。在此功能中,我的应用会询问用户您要共享哪个库中的内容。

我写了下面的代码来分配图像,从默认 library/Custom 库中检索。

应用在具有自定义单元格的集合视图中显示此图像。

当我 select 应用程序自定义库然后从文档目录中检索图像时,但是 imagewithcontentofFile cosuming 大约 90 到 100 Mb 内存。

在其他情况下,当我 select 应用默认库时,它不会占用超过 8 或 10 Mb 的内存。

我为自定义库尝试了来自堆栈 Q/A 的不同代码,但仍然存在内存问题。

-1-
CGImageRef iref = [[UIImage imageNamed:asset] CGImage] ;
imgVwMediaFile.image=[UIImage  imageWithCGImage:iref];
iref=nil

-2-
NSData *imageData = [[NSData alloc] initWithContentsOfFile:path];
imgVwMediaFile.image=[UIImage imageWithData:imageData];
imageData=nil

-3-
imgVwMediaFile.image=[UIImage imageNamed:path];

所以请指导我如何从文档目录加载图像?

在不增加内存负载的情况下从文档目录加载图像的最佳方法是什么?

问题是您保存到磁盘的图像很大,因此加载大量图像以在集合中显示会占用大量内存。您使用内置库中的缩略图,因此您会看到不同的结果。

Ideally you would save a folder of thumbnails in your custom library and display those, then, when an image is selected, get the corresponding full size image to use (this is the approach basically all photo libraries use).或者,您可以即时调整图像的大小,但您的滚动基本上肯定会很糟糕。

    UIImage *img = [UIImage imageWithContentsOfFile:path];