*在给应用截图是发现系统截图方法不能截取播放器的画面,所以需要手动截取视频当前帧的画面。
本文说明的是AVPlayer的视频流截图方法。
截取视频画面总共分三步
一、声明所需属性
@interface ViewController ()
{
AVPlayer *_player;
AVPlayerItemVideoOutput *_videoOutPut;
}
@end
二、实例化
//初始化输出流
_videoOutPut = [[AVPlayerItemVideoOutput alloc] init];
//初始化播放地址
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:@"视频地址"]];
//添加输出流
[item addOutput:_videoOutPut];
//初始化播放器
_player = [[AVPlayer alloc] initWithPlayerItem:item];
//展示播放器到视图上。。。。
三、获取关键帧画面
-(void)getCurrentImage
{
CMTime itemTime = _player.currentItem.currentTime;
CVPixelBufferRef pixelBuffer = [_videoOutPut copyPixelBufferForItemTime:itemTime itemTimeForDisplay:nil];
CIImage *ciImage = [CIImage imageWithCVPixelBuffer:pixelBuffer];
CIContext *temporaryContext = [CIContext contextWithOptions:nil];
CGImageRef videoImage = [temporaryContext
createCGImage:ciImage
fromRect:CGRectMake(0, 0,
CVPixelBufferGetWidth(pixelBuffer),
CVPixelBufferGetHeight(pixelBuffer))];
//当前帧的画面
UIImage *currentImage = [UIImage imageWithCGImage:videoImage];
CGImageRelease(videoImage);
}