如何以编程方式捕获播放video的屏幕截图?

我想在xcode中捕获播放video的屏幕截图。 我怎么能以编程方式做到这一点? 我在apple文档中找到了一个截图方法如下。 如果我只是想截取视图的截图,该方法可以正常工作。 但是,它无法使用播放video。

- (UIImage*)screenshot { // Create a graphics context with the target size // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext CGSize imageSize = [[UIScreen mainScreen] bounds].size; if (NULL != UIGraphicsBeginImageContextWithOptions) UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0); else UIGraphicsBeginImageContext(imageSize); CGContextRef context = UIGraphicsGetCurrentContext(); // Iterate over every window from back to front for (UIWindow *window in [[UIApplication sharedApplication] windows]) { if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) { // -renderInContext: renders in the coordinate space of the layer, // so we must first apply the layer's geometry to the graphics context CGContextSaveGState(context); // Center the context around the window's anchor point CGContextTranslateCTM(context, [window center].x, [window center].y); // Apply the window's transform about the anchor point CGContextConcatCTM(context, [window transform]); // Offset by the portion of the bounds left of and above the anchor point CGContextTranslateCTM(context, -[window bounds].size.width * [[window layer] anchorPoint].x, -[window bounds].size.height * [[window layer] anchorPoint].y); // Render the layer hierarchy to the current context [[window layer] renderInContext:context]; // Restore the context CGContextRestoreGState(context); } } // Retrieve the screenshot image UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } 

我需要捕捉播放video的屏幕截图。 任何想法?

希望这段代码能有所帮助

捕获播放video的屏幕截图

 - (void)setupVideoPlayerView { videoPlayerVC = [[AVPlayerViewController alloc] init]; videoPlayerVC.view.frame = self.videoPlayerView.bounds; videoPlayerVC.videoGravity = AVLayerVideoGravityResizeAspectFill [videoPlayerView addSubview:_videoPlayerVC.view]; [videoPlayerVC setShowsPlaybackControls:NO]; videoPlayerVC.player = [AVPlayer playerWithURL:[NSURL URLWithString:kVideoURL]; [videoPlayerVC.player play]; } - (UIImage *)snapImageFromRuningVideo { AVAsset *assest = [self.videoPlayerVC.player.currentItem asset]; AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:assest]; CGImageRef imageRef = [imageGenerator copyCGImageAtTime:self.videoPlayerVC.player.currentItem.currentTime actualTime:nil error:nil]; UIImage *screengrab = [UIImage imageWithCGImage:imageRef]; return screengrab; } 

这里只需创建AVPlayerViewController,如果你想要控制器栏或只是实例化AVPlayer并绑定video和播放的URL。

无论何时你想在按钮点击快照时捕获正在运行的video的快照,你都可以调用方法“snapImageFromRuningVideo”,它将返回UIImage对象引用。

AVPlayer在内部创建AVPlayerItem和AVAsset对象。 现在只需使用AVAsset对象创建AVAssetImageGenerator对象,然后通过调用“CGImageRef imageRef = [imageGenerator copyCGImageAtTime:[imageGenerator copyCGImageAtTime:self.videoPlayerVC.player.currentItem.currentTime actualTime:nil error:nil]从您想拍摄快照的时间开始复制图像;”

在这里我们必须在拍摄快照的瞬间传递玩家当前时间