Question

I am writing an app that is a clone of the UIImagePicker but uses the Assets library. When the user selects a photo, it takes a little bit too long for the image to load. I notice that when I use the photos app which has identical functionality as to what I'm developing, that the image loading is a bit faster. I've heard another responder on this site mention the following in order to mimic the functionality of the photos app:

"Load the thumbnail image first (best with dispatch_async) - that should be really quick. When this has completed, load the fullscreen image like you did above. This is what apple does in the Photo App to provide a smooth user experience."

Does anyone have any code samples of how this can be accomplished? I'm not quite sure that I understand what he means.

Also here is my code for which I'm using to load an image (I'm passing the image as a parameter to another view controller):

myImage = [UIImage imageWithCGImage:[[myAsset defaultRepresentation] fullScreenImage]];
Was it helpful?

Solution

The class ALAsset has two methods to obtain thumbnails:

- (CGImageRef)thumbnail
- (CGImageRef)aspectRatioThumbnail

I bet they are faster than obtaining the full screen sized version of the asset.

Also, you can wrap them with an async operation. Be sure to update the UI in main thread. Roughly like this:

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];
});

If you need to obtain thumbnails for videos you should use AVAssetImageGenerator. It has a method to obtain them asynchronously.

Look for Apple sample code (AVEditDemo and probably others working with assets library).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top