1.iOS9.0以前的截图
1.1框架
#import <MediaPlayer/MediaPlayer.h>
类:MPMoviePlayerController
1.1步骤
1.注册截屏通知
2.调用截屏方法(发出截图请求)
3.在通知中获得截取的图片
1.2使用
// 监听截图通知
-(void)registerNotification{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(thumbnailImageRequestDidFinishNotification:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:nil];
}
// 截图的通知响应方法
- (void)thumbnailImageRequestDidFinishNotification:(NSNotification *)notication
{
UIImage *image = notication.userInfo[MPMoviePlayerThumbnailImageKey];
self.imageView.image = image;
}
//监听取消
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:nil];
}
//调用截屏方法(发出截图请求)
- (void)snipVideoView{
// 截图请求
NSTimeInterval interval = self.mpc.currentPlaybackTime;
// 通过通知来获取截图
[self.mpc requestThumbnailImagesAtTimes:@[@(interval)] timeOption:MPMovieTimeOptionExact];
}
2.iOS9.0以后的截图
2.1框架
#import <AVFoundation/AVFoundation.h>
类:AVAssetImageGenerator
2.2步骤
1.获得视频路径
2.创建视频资源截图工具
3.获得某个时间的截图
4.使用图片
2.3使用
- (void)snipVideoView{
//1.获得视频路径
NSURL *url = [[NSBundle mainBundle] URLForResource:@"video3.mp4" withExtension:nil];
//2.视频资源
AVAsset *asset = [AVAsset assetWithURL:url];
//3.视频资源截图工具
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
//4.获得某个时间的截图
//4.1当前正在播放的秒 , 视频的帧率
CMTime time = CMTimeMakeWithSeconds(self.mpc.currentPlaybackTime, asset.duration.timescale);
//4.2将CMTime转换成NSValue
NSValue *value = [NSValue valueWithCMTime:time];
//4.3生成CMTime对应时间的截图
[generator generateCGImagesAsynchronouslyForTimes:@[value] completionHandler:^(CMTime requestedTime, CGImageRef _Nullable image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * _Nullable error) {
//5.使用图片
//5.1注意:要对这个image进行强引用,防止被释放
CGImageRetain(image);
//5.2回到主线程更新UI
dispatch_async(dispatch_get_main_queue(), ^{
//5.3赋值图片
UIImage *resultImage = [UIImage imageWithCGImage:image];
self.imageView.image = resultImage;
//5.4释放图片
CGImageRelease(image);
});
}];
}