在后台线程上从 ALAssetRepresentation 获取 fullScreenImage
我需要使用 ALAssets 中的 UIImage 实例填充多个 UIImageView 实例(大约 10 个)。我不想在执行此操作时锁定主线程,因此希望在后台线程中尽可能多地执行操作。从 ALAsset 获取 CGImage 是最耗时的,所以我想将其放在后台线程中。
我遇到的问题是只有第一张图像实际上被正确加载。任何其他 UIImageView 实例最终都会为空。
下面是我的(简化的)代码。 processAssets 方法迭代资产数组,并在后台线程上调用 loadCGImage。此方法从 ALAsset 获取 fullScreenImage 并将其传递给主线程上的 populateImageView,后者使用它来生成 UIImage 并填充 UIImageView。
- (void)processAssets {
for(int i = 0; i < [assetArr count]; i++){
ALAsset *asset = [assetArr objectAtIndex:i];
[self performSelectorInBackground:@selector(loadCGImage:) withObject:asset];
}
}
- (void)loadCGImage:(ALAsset *)asset
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CGImageRef imgRef = CGImageRetain([[asset defaultRepresentation] fullScreenImage]);
[self performSelectorOnMainThread:@selector(populateImageView:) withObject:imgRef waitUntilDone:YES];
CGImageRelease(imgRef);
[pool release];
}
- (void)populateImageView:(CGImageRef)imgRef
{
UIImage *img = [[UIImage imageWithCGImage:imgRef] retain];
UIImageView *view = [[UIImageView alloc] initWithImage:image];
}
我不知道为什么这不能正常工作。有什么想法吗?
I need to populate a number of UIImageView instances (about 10) with UIImage instances from ALAssets. I don't want to lock up the main thread while doing that so want to do as much as possible in a background thread. Getting the CGImage from the ALAsset is the most time consuming, so I would like to put that in a background thread.
The problem I'm having is that only the first image actually gets loaded properly. Any other UIImageView instances end up empty.
Below is my (simplified) code. The processAssets method iterates through an array of assets, and calls loadCGImage on a background thread. This methods gets the fullScreenImage from the ALAsset and passes it on to populateImageView on the main thread, which uses it to generate a UIImage and populate the UIImageView.
- (void)processAssets {
for(int i = 0; i < [assetArr count]; i++){
ALAsset *asset = [assetArr objectAtIndex:i];
[self performSelectorInBackground:@selector(loadCGImage:) withObject:asset];
}
}
- (void)loadCGImage:(ALAsset *)asset
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CGImageRef imgRef = CGImageRetain([[asset defaultRepresentation] fullScreenImage]);
[self performSelectorOnMainThread:@selector(populateImageView:) withObject:imgRef waitUntilDone:YES];
CGImageRelease(imgRef);
[pool release];
}
- (void)populateImageView:(CGImageRef)imgRef
{
UIImage *img = [[UIImage imageWithCGImage:imgRef] retain];
UIImageView *view = [[UIImageView alloc] initWithImage:image];
}
I am not sure why this isn't working properly. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你应该尝试这样的事情(使用块)
干杯,
亨德里克
You should try something like this (using blocks)
Cheers,
Hendrik