在后台线程上从 ALAssetRepresentation 获取 fullScreenImage

发布于 2024-11-16 18:23:13 字数 1179 浏览 5 评论 0原文

我需要使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

々眼睛长脚气 2024-11-23 18:23:13

你应该尝试这样的事情(使用块)

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    //load the fullscreenImage async
    dispatch_async(dispatch_get_main_queue(), ^{
      //assign the loaded image to the view.
    });
});

干杯,

亨德里克

You should try something like this (using blocks)

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    //load the fullscreenImage async
    dispatch_async(dispatch_get_main_queue(), ^{
      //assign the loaded image to the view.
    });
});

Cheers,

Hendrik

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文