AVPlayer和视频的屏幕截图 [英] Screenshot for AVPlayer and Video

查看:273
本文介绍了AVPlayer和视频的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在更大的视图中截取 AVPlayer 的屏幕截图。我想只构建一个测试框架,所以私有 API 或任何方法都是好的,因为在发布到AppStore时不会包含框架。

I am trying to take a screenshot of an AVPlayer inside a bigger view. I want to build a testing framework only, so private APIs or any method is good, because the framework will not be included when releasing to the AppStore.

我尝试过使用


  • UIGetScreenImage( ):在模拟器上运行良好但在设备上运行不正确

  • snapshotviewafterscreenupdates :它显示视图但我不能从中创建一个 UIImage

  • drawViewInHierarchy renderInContext 不适用于 AVPlayer

  • 我不想使用 AVAssetGenerator 或从视频中获取图像,很难获得良好的坐标或视频播放器作为其他视图的子视图

  • UIGetScreenImage() : works well on simulator but not on device
  • snapshotviewafterscreenupdates: it shows the view but I cannot create a UIImage from that.
  • drawViewInHierarchy and renderInContext will not work with AVPlayer
  • I don't want to use AVAssetGenerator or getting image from video, it is hard to get a good coordinate or the video player as the subview of other views

推荐答案

我知道你不想使用AVAssetImageGenerator,但我也对此进行了广泛的研究,我相信目前唯一的解决方案是使用AVAssetImageGenerator。你说要获得正确的坐标并不困难,因为你应该能够获得你的玩家的当前时间。在我的应用程序中,以下代码完美运行:

I know you don't want to use the AVAssetImageGenerator but I've also researched this extensively and I believe the only solution currently is using the AVAssetImageGenerator. It's not that difficult as you say to get the right coordinate because you should be able to get the current time of your player. In my App the following code works perfectly:

-(UIImage *)getAVPlayerScreenshot 
{
    AVURLAsset *asset = (AVURLAsset *)self.playerItem.asset;
    AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    imageGenerator.requestedTimeToleranceAfter = kCMTimeZero;
    imageGenerator.requestedTimeToleranceBefore = kCMTimeZero;
    CGImageRef thumb = [imageGenerator copyCGImageAtTime:self.playerItem.currentTime
                                              actualTime:NULL
                                                   error:NULL];
    UIImage *videoImage = [UIImage imageWithCGImage:thumb];
    CGImageRelease(thumb);
    return videoImage;
}

这篇关于AVPlayer和视频的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆