iOS截取视频某一帧图片(关键帧,AVAssetImageGenerator)

获取第一帧图片

导入 AVFoundation.Framework、CoreMedia.Framework

实现代码如下:

+ (UIImage*) thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time {
    AVURLAsset *asset = [[[AVURLAsset alloc] initWithURL:videoURL options:nil] autorelease];
    NSParameterAssert(asset);
    AVAssetImageGenerator *assetImageGenerator = [[[AVAssetImageGenerator alloc] initWithAsset:asset] autorelease];
    assetImageGenerator.appliesPreferredTrackTransform = YES;
    assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;

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

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

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

    return thumbnailImage;
}

获取多帧图片的办法

[mImageGenerator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:CMTimeMakeWithSeconds(time, NSEC_PER_SEC)]] completionHandler:
    ^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error)
    {
 
        NSLog(@"actual got image at time:%f", CMTimeGetSeconds(actualTime));
        if (image)
        {
            [CATransaction begin];
            [CATransaction setDisableActions:YES];
            [layer setContents:(id)image];
 
            //UIImage *img = [UIImage imageWithCGImage:image];
            //UIImageWriteToSavedPhotosAlbum(img, self, nil, nil);
 
            [CATransaction commit];
        }
    }];
 


iOS截取视频某一帧图片(关键帧,AVAssetImageGenerator),布布扣,bubuko.com

iOS截取视频某一帧图片(关键帧,AVAssetImageGenerator)

上一篇:IOS使用 Visual Format Language 定义水平和垂直约束


下一篇:下载Google官方/CM Android源代码自动重新开始的Shell脚本