首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何以编程方式捕捉播放视频的截图?

如何以编程方式捕捉播放视频的截图?
EN

Stack Overflow用户
提问于 2013-10-24 18:07:49
回答 1查看 2.5K关注 0票数 2

我想用xcode捕捉一个播放视频的截图。我如何以编程的方式完成它?我在苹果文档中找到了一个截图方法,如下所示。如果我只想拍摄一个视图的截图,这个方法很好。但是,它不能处理播放的视频。

代码语言:javascript
复制
- (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;
}

我需要捕捉一个播放视频的截图。有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2016-04-18 12:39:57

希望这段代码能帮上忙

捕捉播放视频的截图

代码语言:javascript
复制
- (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和绑定的网址的视频和播放。

无论何时,只要您想要在快照按钮上捕获正在运行的视频的快照,就可以调用方法"snapImageFromRuningVideo“,它将返回UIImage对象引用。

AVPlayer内部创建AVPlayerItem和AVAsset对象。现在只使用copyCGImageAtTime:self.videoPlayerVC.player.currentItem.currentTime对象创建AVAssetImageGenerator对象,然后通过调用"CGImageRef imageRef = imageGenerator实际时间:nil error:nil;“,每次复制快照时开始复制图像。

在这里,我们必须在抓取快照的瞬间传递播放器当前的时间。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19573345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档