微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在iOS上快速将ALAsset映像保存到磁盘?

我正在使用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,已使用UIImageJPEGRepresentation替换了UIImagePNGRepresentation.
对于解决方案2,已将kUTTypePNG替换为kUTTypeJPEG.

另外值得注意的是,第二种解决方案比第一种解决方案更有效.

解决方法

这是因为PNG压缩是一个缓慢的过程,并且在iPhone的处理器上需要一段时间,特别是对于全尺寸摄影.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐