使用UIImagePNGRepresentation时出现内存问题 [英] Memory issue in using UIImagePNGRepresentation

查看:488
本文介绍了使用UIImagePNGRepresentation时出现内存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我发现此模块很麻烦.我从Photolibrary导入了100多个图像,并将它们以不同的名称保存在documents目录中.不出所料,我在不寻常的地方遇到了内存问题.看来UIImagePNGRepresenation正在缓存文件.因此,当我对300多个图像运行以下过程时,我看到3.00 GB范围内的总字节数",并且由于内存(在分配工具中测试)而崩溃.我已经粘贴了下面的代码.这段代码是否还有其他替代方法

Hi Guys I found this module to be troublesome. I import more than 100 images from Photolibrary, save them in documents directory with a different name. As expected I had a memory issue in the unusual place. It seems UIImagePNGRepresenation is caching files. So when I run the below process for 300+ images, I see "Overall bytes" in the range of 3.00 GB and crashes due to Memory (tested in allocations tool). I have pasted the code below. Is there any alternative for this code

-(void)something
{
   NSData *data=nil;
   for (int i=0; i<numberOfImages; i++) {

    @autoreleasepool {

        UIImage *image=[UIImage imageNamed:[NSString stringWithFormat:@"image%d.png",i]];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

        NSString *documentsDirectory = [[paths objectAtIndex:0] stringByAppendingString:@"directoryname"];

        NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"image%d.png",i]];

        //convert image into .png format
        data=UIImagePNGRepresentation(image);
        [data writeToURL:[NSURL URLWithString:fullPath] atomically:NO];
      }
   }
   data=nil;
}

推荐答案

我将此问题邮寄给Apple,他们要求我在每次分配之间引入睡眠周期.在分配之前添加睡眠.

I mailed this issue to Apple and they asked me to introduce sleep cycles between every allocation. Add sleep before allocation.

这篇关于使用UIImagePNGRepresentation时出现内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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