【问题标题】:How can I programmatically capture a screenshot of a playing video?如何以编程方式捕获正在播放的视频的屏幕截图?
【发布时间】:2013-10-24 18:07:49
【问题描述】:

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

- (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;
}

我需要截取正在播放的视频的屏幕截图。有什么想法吗?

【问题讨论】:

标签: ios objective-c video screenshot


【解决方案1】:

希望这段代码能有所帮助

捕获正在播放的视频的屏幕截图

- (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 并绑定视频的 URL 并播放。

当您想在单击快照按钮时捕获当时正在运行的视频的快照时,您可以调用方法“snapImageFromRuningVideo”,该方法将返回 UIImage 对象引用。

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

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

【讨论】:

  • 请详细说明这段代码是如何回答这个问题的。
  • 希望现在的解释能给你正确的想法
  • @Rajesh 它有效!如果我想与 Avplayer 视频一起捕获整个屏幕怎么办?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-11
  • 1970-01-01
相关资源
最近更新 更多