【发布时间】:2012-03-04 20:45:22
【问题描述】:
我正在使用 ALAsset 来检索这样的图像:
[[asset defaultRepresentation] fullResolutionImage]]
这会返回 CGImageRef,我想尽快将其保存到磁盘...
解决方案 1:
UIImage *currentImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
NSData *currentImageData = UIImagePNGRepresentation(currentImage);
[currentImageData writeToFile:filePath atomically:YES];
解决方案 2:
CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:filePath];
CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypePNG, 1, NULL);
CGImageDestinationAddImage(destination, [[asset defaultRepresentation] fullResolutionImage], nil);
CGImageDestinationFinalize(destination);
问题在于这两种方法在设备上的执行速度都非常慢。每张图片我需要大约 2 秒来执行此操作。这绝对是太长了。
问题:如何加快图像保存过程?或者也许有更好的解决方案?
更新:
这两种解决方案的最佳性能改进是将图像保存为 JPEG 格式而不是 PNG。
因此,对于解决方案 1,已将 UIImagePNGRepresentation 替换为 UIImageJPEGRepresentation。
对于解决方案 2,已将 kUTTypePNG 替换为 kUTTypeJPEG。
另外值得注意的是,第二种解决方案比第一种解决方案更节省内存。
【问题讨论】:
标签: ios image save disk alasset