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.
I have tried with using
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
AVAssetGenerator
for getting image from video, it is hard to get a good coordinate or the video player as the subview of other viewsI 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 rending videos using GPU, so you cannot capture it using core graphics methods.
However that’s possible to capture images with AVAssetImageGenerator
, you need specify a CMTime.
Update:
Forget to take a screenshot of the entire screen. AVPlayerItemVideoOutput
is my final choice, it supports video steam.
Here is my full implementation: https://github.com/BB9z/ZFPlayer/commit/a32c7244f630e69643336b65351463e00e712c7f#diff-2d23591c151edd3536066df7c18e59deR448
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With