UIImagePickerController从图书馆接收的视频缩略图

时间:2011-04-19 15:51:31

标签: video uiimagepickercontroller thumbnails

我正在尝试使用UIImagePickerController获取从库中拾取的视频缩略图。

这是我的代码。如果我使用相机录制视频,我可以获得视频的缩略图。但是如果我从图书馆中获取视频,我就无法获得缩略图。有谁知道为什么?

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

    if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
    {
        NSURL *mediaUrl = [info objectForKey:UIImagePickerControllerMediaURL];

        MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:mediaUrl];
        moviePlayer.shouldAutoplay = NO;
        UIImage *thumbnail = [[moviePlayer thumbnailImageAtTime:0.0 timeOption:MPMovieTimeOptionNearestKeyFrame] retain];
        [imageView setImage:thumbnail];  //imageView is a UIImageView

        [moviePlayer release];
        [thumbnail release];
        [self dismissModalViewControllerAnimated:YES];
    }
}

5 个答案:

答案 0 :(得分:42)

我有一个半相关的问题,并最终放弃使用MPMoviePlayer生成缩略图。请尝试使用AVAssetImageGenerator。 Apple讨论了如何使用AVAssetImageGenerator创建缩略图here。这是我自己的示例代码,它抓取单个缩略图图像。您需要包含AVFoundation框架。

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

答案 1 :(得分:15)

AssetsLibrary框架完全符合ALAssetsLibraryALAsset类所需的内容。此代码适用于照片和视频以及缩略图,与您在选择器中看到的完全相同。

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

请注意iOS 8上有Photo Stream相册存在错误,有关解决方法,请参阅ALAssetsLibrary assetForURL: always returning nil for photos in “My Photo Stream” in iOS 8.1

答案 2 :(得分:3)

Swift 3.1

我创建了一个类函数并在param中传递你的url

class func captureThumbnail(withVideoURL videoURL:URL,secs:Int,preferredTimeScale scale:Int,completionHandler:((UIImage?) ->Void)?) -> Void
{
    //let seconds : Int64 = 10
    // let preferredTimeScale : Int32 = 1

    DispatchQueue.global().async {

        do
        {
            let asset = AVURLAsset(url: videoURL)

            let imgGenerator = AVAssetImageGenerator(asset: asset)
            imgGenerator.appliesPreferredTrackTransform = true
            let cgImage = try imgGenerator.copyCGImage(at:CMTimeMake(Int64(secs), Int32(scale)),actualTime: nil)
            let thumbnail = UIImage(cgImage: cgImage)
            completionHandler?(thumbnail)
        }
        catch let error as NSError
        {
            print("Error generating thumbnail: \(error)")
            completionHandler?(nil)
        }
    }
}

答案 3 :(得分:3)

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
    }
}

答案 4 :(得分:2)

Swift 5 及以上

func thumbnailForVideo(url: URL) -> UIImage? {
    let asset = AVAsset(url: url)
    let assetImageGenerator = AVAssetImageGenerator(asset: asset)
    assetImageGenerator.appliesPreferredTrackTransform = true

    var time = asset.duration
    time.value = min(time.value, 2)

    do {
        let imageRef = try assetImageGenerator.copyCGImage(at: time, actualTime: nil)
        return UIImage(cgImage: imageRef)
    } catch {
        print("failed to create thumbnail")
        return nil
    }
}