开发者

Getting fullScreenImage from ALAssetRepresentation on background thread

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 wai开发者_JAVA百科tUntilDone: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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜