截取网络视频图片

  if (!_player) {
    //初始化输出流
    _videoOutPut = [[AVPlayerItemVideoOutput alloc] init];
    //初始化播放地址
    //http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4
    AVPlayerItem *item = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:@"http://192.168.1.160:81/stream"]];
    //添加输出流
    [item addOutput:_videoOutPut];
    //初始化播放器
    _player = [[AVPlayer alloc] initWithPlayerItem:item];
    _player.volume = 0;
    //显示画面
    AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:_player];
    //视频填充模式
    layer.videoGravity = AVLayerVideoGravityResizeAspect;
    //设置画布frame
    layer.frame = CGRectMake(0, 0, mWidth, mHeight);
    //添加到当前视图
    [self.view.layer addSublayer:layer];
  }
  //开始播放
  [_player play];
-(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];
  NSData *imgData = UIImageJPEGRepresentation(currentImage, 0.5);
  //当前的图片
  NSString *savedImagePath = [NSString stringWithFormat:@"%@/robot.jpg",_documentPath];
  [imgData writeToFile:savedImagePath atomically:YES];
  [[NSNotificationCenter defaultCenter] postNotificationName:@"CameraAreadyNotif" object:nil];
  CGImageRelease(videoImage);
}

你可能感兴趣的:(截取网络视频图片)