Memory 使用UIImagePngResentation时内存问题

Memory 使用UIImagePngResentation时内存问题,memory,uiimage,uiimagepngrepresentation,Memory,Uiimage,Uiimagepngrepresentation,我发现这个模块很麻烦。我从Photolibrary导入了100多幅图像,并将它们以不同的名称保存在documents目录中。正如所料,我在一个不寻常的地方出现了记忆问题。UIImagePngRepresation似乎正在缓存文件。因此,当我对300多个图像运行下面的过程时,我看到“总字节”在3.00GB的范围内,并且由于内存(在分配工具中测试)而崩溃。我已将代码粘贴到下面。这个代码有其他选择吗 -(void)something { NSData *data=nil; for (int

我发现这个模块很麻烦。我从Photolibrary导入了100多幅图像,并将它们以不同的名称保存在documents目录中。正如所料,我在一个不寻常的地方出现了记忆问题。UIImagePngRepresation似乎正在缓存文件。因此,当我对300多个图像运行下面的过程时,我看到“总字节”在3.00GB的范围内,并且由于内存(在分配工具中测试)而崩溃。我已将代码粘贴到下面。这个代码有其他选择吗

-(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;
}
-(无效)某物
{
NSData*数据=零;

对于(int i=0;i缓存来自
[UIImage imageNamed://code>,而不是
UIImagePNGRepresentation()
。请改为执行以下操作:

NSString *imageName = [NSString stringWithFormat:@"image%d.png", i];
NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageName];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];

...

我把这个问题发到苹果公司,他们要求我在每次分配之间引入睡眠周期。在分配之前添加睡眠。

我通过发送4通道图像(RGBA或RGBX)而不是3通道图像(RGB)来解决这个问题


您可以检查是否有机会更改图像的参数。

没有。它不起作用。我尝试了[UIImage imageWithContentsOfFile:],[UIImage alloc]initWithContentsOfFile:]。问题是在将UIImage转换为NSData的过程中。我检查了上述15次OK,很明显,您没有使用这些映像,而且它们是PNG,为什么不直接复制它们?
[NSFileManager copyItemAtPath:toPath:error:]
好主意..!我现在会检查它是否不起作用。资产URL与普通URL不同。您可以使用ALAssetRepresentation复制原始数据。缺点是照片通常是JPEG格式,而不是PNG格式。我也尝试过这一点,但没有帮助。请提供其他解决方法…提前感谢