AVPlayer 和视频的屏幕截图 [英] Screenshot for AVPlayer and Video
问题描述
我正在尝试在更大的视图中截取 AVPlayer
的屏幕截图.我只想构建一个测试框架,所以私有的APIs
或者任何方法都可以,因为发布到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 devicesnapshotviewafterscreenupdates
: it shows the view but I cannot create aUIImage
from that.drawViewInHierarchy
andrenderInContext
will not work withAVPlayer
- I don't want to use
AVAssetGenerator
for 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屋!