*在給應(yīng)用截圖是發(fā)現(xiàn)系統(tǒng)截圖方法不能截取播放器的畫面,所以需要手動截取視頻當(dāng)前幀的畫面。
本文說明的是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];
- //展示播放器到視圖上。。。。
三、獲取關(guān)鍵幀畫面
- -(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))];
-
- //當(dāng)前幀的畫面
- UIImage *currentImage = [UIImage imageWithCGImage:videoImage];
- CGImageRelease(videoImage);
- }
更多建議: