我正在使用ALAsset来检索图像,就像这样:
这个返回CGImageRef,我希望尽快将其保存到磁盘上...
解决方案1:
问题在于这两种方法在设备上执行速度非常慢。每张图像的处理时间约为2秒。这显然太长了。
问题:如何加快图像保存过程?或者,是否有更好的解决方案?
更新: 两种解决方案中最佳的性能改进是将图像保存为JPEG格式,而不是PNG格式。 因此,对于解决方案1,我们已经用UIImageJPEGRepresentation替换了UIImagePNGRepresentation。 对于解决方案2,我们已经用kUTTypeJPEG替换了kUTTypePNG。
值得注意的是,第二种解决方案比第一种更省内存。
[[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,我们已经用UIImageJPEGRepresentation替换了UIImagePNGRepresentation。 对于解决方案2,我们已经用kUTTypeJPEG替换了kUTTypePNG。
值得注意的是,第二种解决方案比第一种更省内存。