UIImagePickerController thumbnail of video which is pick up from library

I had a semi-related problem, and eventually abandoned using MPMoviePlayer to generate thumbnails. Try using AVAssetImageGenerator instead. Apple discusses using AVAssetImageGenerator to create thumbnails here. Here is my own sample code, which grabs a single thumbnail image. You will need to include the AVFoundation framework.

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:vidPath options:nil];
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
gen.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMakeWithSeconds(0.0, 600);
NSError *error = nil;
CMTime actualTime;

CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
[gen release];

The AssetsLibrary framework has exactly what you need with the ALAssetsLibrary and ALAsset classes. This code work for both photos and videos and the thumbnail image is exactly the same as you see in the picker.

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [[ALAssetsLibrary new] assetForURL:info[UIImagePickerControllerReferenceURL] resultBlock:^(ALAsset *asset) {
        imageView.image = [UIImage imageWithCGImage:asset.thumbnail];
    } failureBlock:^(NSError *error) {
        // handle error
    }];
    [self dismissViewControllerAnimated:YES completion:nil];
}

Note that there is a bug on iOS 8 with Photo Stream albums, see ALAssetsLibrary assetForURL: always returning nil for photos in “My Photo Stream” in iOS 8.1 for a workaround.


func videoSnapshot(filePathLocal:URL) -> UIImage? {
    do
    {
        let asset = AVURLAsset(url: filePathLocal)
        let imgGenerator = AVAssetImageGenerator(asset: asset)
        imgGenerator.appliesPreferredTrackTransform = true
        let cgImage = try imgGenerator.copyCGImage(at:CMTimeMake(Int64(0), Int32(1)),actualTime: nil)
        let thumbnail = UIImage(cgImage: cgImage)
        return thumbnail
    }
    catch let error as NSError
    {
        print("Error generating thumbnail: \(error)")
        return nil
    }
}