我正在编写一个应用程序,该应用程序是 UIImagePicker 的克隆但使用 Assets 库。当用户选择一张照片时,加载图像的时间有点太长了。我注意到当我使用与我正在开发的功能相同的照片应用程序时,图像加载速度会更快一些。我听说该网站上的另一个响应者提到以下内容以模仿照片应用程序的功能:
“首先加载缩略图(最好使用 dispatch_async)- 这应该非常快。完成后,像上面那样加载全屏图像。这就是苹果在照片应用程序中所做的,以提供流畅的用户体验”
有没有人有关于如何实现这一点的代码示例?我不太确定我是否理解他的意思。
这也是我用来加载图像的代码(我将图像作为参数传递给另一个 View Controller ):
myImage = [UIImage imageWithCGImage:[[myAsset defaultRepresentation] fullScreenImage]];
最佳答案
ALAsset
类有两种获取缩略图的方法:
- (CGImageRef)thumbnail
- (CGImageRef)aspectRatioThumbnail
我打赌它们比获取 Assets 的全屏大小版本更快。
此外,您可以使用异步操作包装它们。请务必在主线程中更新 UI。大致是这样的:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
/* obtain the image here */
dispatch_async(dispatch_get_main_queue(), ^{
/* update screen here */
});
[pool drain];
});
如果您需要获取视频的缩略图,您应该使用 AVAssetImageGenerator
。它有一个异步获取它们的方法。
寻找 Apple 示例代码(AVEditDemo 和其他可能使用 Assets 库的代码)。
关于iphone - 如何使用 Assets Library 加速图像加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7856692/