视频截图获取视频某一帧做图片 ╰半橙微兮° 2022-03-16 07:54 316阅读 0赞 本地存储的视频文件,通过某些方法获取该视频的首帧图片,或是某个时间的图片,或是该视图不同时间段的多张图片。 获取视频第一帧的图片,示例代码如下: + (UIImage *)videoThumbnailWithFilePath:(NSString *)filePath { if (filePath == nil || filePath.length <= 0) { return nil; } NSURL *url = [NSURL fileURLWithPath:filePath]; AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil]; AVAssetImageGenerator *assetGen = [[AVAssetImageGenerator alloc] initWithAsset:asset]; assetGen.appliesPreferredTrackTransform = YES; CMTime time = CMTimeMakeWithSeconds(0.0, 600); NSError *error = nil; CMTime actualTime; CGImageRef image = [assetGen copyCGImageAtTime:time actualTime:&actualTime error:&error]; UIImage *videoImage = [[UIImage alloc] initWithCGImage:image]; CGImageRelease(image); return videoImage; } > 需要导入头文件’\#import <AVFoundation/AVFoundation.h>’ 获取视频某个时间的图片,示例代码如下: + (UIImage *)videoThumbnailWithFilePath:(NSString *)filePath atTime:(NSTimeInterval)time { if (filePath == nil || filePath.length <= 0) { return nil; } NSURL *url = [NSURL fileURLWithPath:filePath]; MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url]; moviePlayer.shouldAutoplay = NO; UIImage *videoImage = [moviePlayer thumbnailImageAtTime:time timeOption:MPMovieTimeOptionNearestKeyFrame]; return videoImage; } > 需要导入头文件’\#import <MediaPlayer/MediaPlayer.h>’ 获取视频不同时间的多张图片,示例代码如下: + (void)videoThumbnailWithFilePath:(NSString *)filePath count:(NSInteger)count complete:(void(^)(BOOL success, NSArray *images))complete { if (filePath == nil || filePath.length <= 0) { if (complete) { complete(NO, nil); } } else { __block BOOL isSuccess = NO; __block NSMutableArray *images = [[NSMutableArray alloc] init]; count = (count == 0 ? 1 : count); NSURL *url = [NSURL fileURLWithPath:filePath]; AVAsset *asset = [AVAsset assetWithURL:url]; [asset loadValuesAsynchronouslyForKeys:@[@"duration"] completionHandler:^{ AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset]; // 如果是CGSizeMake(480,136),则获取到的图片是{240, 136}。与实际大小成比例 // generator.maximumSize = CGSizeMake(480,136); // 这个属性保证我们获取的图片的方向是正确的。比如有的视频需要旋转手机方向才是视频的正确方向。 generator.appliesPreferredTrackTransform = YES; /**因为有误差,所以需要设置以下两个属性。如果不设置误差有点大,设置了之后相差非常非常的小**/ generator.requestedTimeToleranceAfter = kCMTimeZero; generator.requestedTimeToleranceBefore = kCMTimeZero; Float64 seconds = CMTimeGetSeconds(asset.duration); NSMutableArray *array = [NSMutableArray array]; for (int i = 0; i < count; i++) { // 想要获取图片的时间位置 CMTime time = CMTimeMakeWithSeconds(i * (seconds / 10.0), 1); [array addObject:[NSValue valueWithCMTime:time]]; } __block int i = 0; [generator generateCGImagesAsynchronouslyForTimes:array completionHandler:^(CMTime requestedTime, CGImageRef _Nullable imageRef, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * _Nullable error) { i++; if (result == AVAssetImageGeneratorSucceeded) { isSuccess = YES; UIImage *image = [UIImage imageWithCGImage:imageRef]; [images addObject:image]; } else { isSuccess = NO; } if (i == count) { dispatch_async(dispatch_get_main_queue(), ^{ if (complete) { complete(isSuccess, images); } }); } }]; }]; } } > 需要导入头文件’\#import <AVFoundation/AVFoundation.h>’
还没有评论,来说两句吧...