我怎样才能捕捉编程播放视频的截图?(How can I programmatically captu

2019-10-18 20:22发布

我想捕捉在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;
}

我需要捕获播放视频的截图。 任何想法?

Answer 1:

希望这PICE的代码将有助于

播放视频的捕捉截屏

- (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对象,然后将图像在同一时间从当你想通过调用“CGImageRef imageRef =取快照复制创建AVAssetImageGenerator对象[:self.videoPlayerVC.player.currentItem.currentTime actualTime:imageGenerator copyCGImageAtTime零错误:零] ;”

在这里,我们有在拍摄快照的即时传递玩家当前时间



文章来源: How can I programmatically capture a screenshot of a playing video?