京东,淘宝App有自己喜欢的商品或者看到什么喜欢的图片可以瞬间截屏。包括视频也是瞬间截屏去分享或者保存。
视频截图首先是针对播放到那一帧了进行视频画面的截取。
创建属性
@property (nonatomic,strong)AVPlayerItemVideoOutput videoOutput;
//初始化
/AVPlayerItemVideoOutput能够处理视频播放过程中像素级别的buffer,使用方法hasNewPixelBufferForItemTime:(CMTime)itemTime可以判断在itemTime 这个时刻是否有可用的像素信息,copyPixelBufferForItemTime: itemTimeForDisplay:可以取到这个时刻的像素CVPixelBufferRef.*/
_videoOutput = [[AVPlayerItemVideoOutput alloc]initWithPixelBufferAttributes:nil];
[self.playerController.player.currentItem addOutput:_videoOutput];
AVPlayerItem提供了AVPlayer播放需要的媒体文件,时间、状态、文件大小等信息,把能监听处理视频的它加入到里面进行获取时间和buff
[self.playerController.player.currentItem addOutput:_videoOutput];
//点击触发截图进行imageView展示(视频)
- (void)btnClick{
CMTime itemTime = [_videoOutput itemTimeForHostTime:CACurrentMediaTime()];
//根据_videoOutput获取当前播放的时间
if ([_videoOutput hasNewPixelBufferForItemTime:itemTime]) {
CVPixelBufferRef pixelBuffer = [_videoOutput copyPixelBufferForItemTime:itemTime itemTimeForDisplay:nil];
CIImage *image = [CIImage imageWithCVPixelBuffer:pixelBuffer];
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgImage = [context createCGImage:image fromRect:[image extent]];
self.upDataImage = [UIImage imageWithCGImage:cgImage];
UIImageView *imageView = [[UIImageView alloc] initWithImage:self.upDataImage];
imageView.frame = CGRectMake(0, 0, kScreenWidth/3, kScreenHeight/3);
[self.view addSubview:imageView];
CVPixelBufferRelease(pixelBuffer);
}
}
普通的截图方法:(界面)
- (UIImage *)convertViewToImage:(UIView *)view
{
@autoreleasepool
{
CGSize size = CGSizeMake(view.width, view.height);
UIGraphicsBeginImageContextWithOptions(size, NO, 2);
[view drawViewHierarchyInRect:view.frame afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}