从拼抢一个的UIImagePickerController视频的第一帧?(Grabbing the

2019-08-17 06:28发布

我想从所选择的视频的第一帧中UIImagePickerController在显示UIImageView ,但我不知道这是否是可能的。 如果是这样,我会怎么做呢?

Answer 1:

您可以通过以下两种方式之一进行。 第一种方法是使用MPMoviePlayerController抢缩图:

MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc]
                                       initWithContentURL:videoURL];
moviePlayer.shouldAutoplay = NO;
UIImage *thumbnail = [moviePlayer thumbnailImageAtTime:time
                     timeOption:MPMovieTimeOptionNearestKeyFrame];

这工作,但MPMoviePlayerController不是特别轻量级的对象,而不是特别快抓住缩略图。

的首选方法是使用新的AVAssetImageGenerator在AVFoundation。 这是速度快,重量轻,比旧方法更灵活。 这里是一个辅助方法,将来自视频返回自动释放的图像。


+ (UIImage *)thumbnailImageForVideo:(NSURL *)videoURL 
                             atTime:(NSTimeInterval)time 
{

    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
    NSParameterAssert(asset);
    AVAssetImageGenerator *assetIG = 
                [[AVAssetImageGenerator alloc] initWithAsset:asset];
    assetIG.appliesPreferredTrackTransform = YES;
    assetIG.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;

    CGImageRef thumbnailImageRef = NULL;
    CFTimeInterval thumbnailImageTime = time;
    NSError *igError = nil;
    thumbnailImageRef = 
             [assetIG copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60)
                             actualTime:NULL
                                  error:&igError];

    if (!thumbnailImageRef)
        NSLog(@"thumbnailImageGenerationError %@", igError );

    UIImage *thumbnailImage = thumbnailImageRef 
                          ? [[UIImage alloc] initWithCGImage:thumbnailImageRef]
                          : nil;

    return thumbnailImage;
}

异步使用


- (void)thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time completion:(void (^)(UIImage *)) completion
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

        AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
        NSParameterAssert(asset);
        AVAssetImageGenerator *assetIG =
        [[AVAssetImageGenerator alloc] initWithAsset:asset];
        assetIG.appliesPreferredTrackTransform = YES;
        assetIG.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;

        CGImageRef thumbnailImageRef = NULL;
        CFTimeInterval thumbnailImageTime = time;
        NSError *igError = nil;
        thumbnailImageRef =
        [assetIG copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60)
                        actualTime:NULL
                             error:&igError];

        if (!thumbnailImageRef)
            NSLog(@"thumbnailImageGenerationError %@", igError );

        UIImage *thumbnailImage = thumbnailImageRef
        ? [[UIImage alloc] initWithCGImage:thumbnailImageRef]
        : nil;

        dispatch_async(dispatch_get_main_queue(), ^{
            completion(thumbnailImage);
        });
    });
}


文章来源: Grabbing the first frame of a video from UIImagePickerController?