Search code examples
iosalassetqlpreviewcontroller

Display an ALAsset using QLPreviewController


I'm wanting to use QLPreviewController to display ALAssets from the Photo Stream QLPreviewController needs an NSURL to display the item

This works great when it is a File URL such as /var/mobile/Applications/5374......9E0/Documents/image33.png

I have the ALAsset However using [[asset defaultRepresentation] url] gives me a NSURL type
assets-library://asset/asset.JPG?id=00000000-0000-0000-0000-000000000075&ext=JPG

But this does not display QLPreviewController just keeps showing loading ??

Any ideas ? Thanks in advance


Solution

  • Maybe not the fastest and most efficient way, but it solves the problem. Please use NSFilemanager instead NSTemporaryDirectory as in documentation :-) Make sure to link against ImageIO.framework

        #import <ImageIO/ImageIO.h>
    
        ....
        NSURL *outURLToUseWithQLPreviewController = nil;
    
        ALAsset *myAsset = ... // received somehow
        ALAssetRepresentation *represent = myAsset.defaultRepresentation;
        CGImageRef representFullScreen = represent.fullScreenImage;
    
        NSString *tempDir = NSTemporaryDirectory();
        NSString *imagePath = [tempDir stringByAppendingPathComponent:represent.filename];
        NSURL *tempURL = [NSURL fileURLWithPath:imagePath];
    
        CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)(tempURL), (__bridge CFStringRef)(represent.UTI), 1, NULL);
        CGImageDestinationAddImage(destination, representFullScreen, nil);
        if ( CGImageDestinationFinalize(destination) ) {
            outURLToUseWithQLPreviewController = tempURL;
        }
        return outURLToUseWithQLPreviewController;