Screenshot for AVPlayer and Video

IosObjective CTestingAvfoundationAvplayer

Ios Problem Overview


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
  • 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

Ios Solutions


Solution 1 - Ios

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;
}

Solution 2 - Ios

Swift version of Bob's answer below. I'm using AVQueuePlayer but should work for regular AVPlayer too.

public func getImageSnapshot() -> UIImage? {
    guard let asset = player.currentItem?.asset else { return nil }
    
    let imageGenerator = AVAssetImageGenerator(asset: asset);
    imageGenerator.requestedTimeToleranceAfter = CMTime.zero;
    imageGenerator.requestedTimeToleranceBefore = CMTime.zero;
    
    do {
        let thumb = try imageGenerator.copyCGImage(at: player.currentTime(), actualTime: nil);
        let image = UIImage(cgImage: thumb);
        return image;
    } catch {
        print("⛔️ Failed to get video snapshot: \(error)");
    }
    return nil;
}

Solution 3 - Ios

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

Solution 4 - Ios

Here is code for taking a screenshot of you entire screen, including the AVPlayer. You only need to add a UIImageView on top of your videoplayer, which stays hidden until we take the screenshot and then we hide it again.

func takeScreenshot() -> UIImage? {
    //1 Hide all UI you do not want on the screenshot
    self.hideButtonsForScreenshot()

    //2 Create an screenshot from your AVPlayer
    if let url = (self.overlayPlayer?.currentItem?.asset as? AVURLAsset)?.url {

          let asset = AVAsset(url: url)

          let imageGenerator = AVAssetImageGenerator(asset: asset)
          imageGenerator.requestedTimeToleranceAfter = CMTime.zero
          imageGenerator.requestedTimeToleranceBefore = CMTime.zero

        if let thumb: CGImage = try? imageGenerator.copyCGImage(at: self.overlayPlayer!.currentTime(), actualTime: nil) {
            let videoImage = UIImage(cgImage: thumb)
            //Note: create an image view on top of you videoPlayer in the exact dimensions, and display it before taking the screenshot
            // mine is created in the storyboard
            // 3 Put the image from the screenshot in your screenshotPhotoView and unhide it
            self.screenshotPhotoView.image = videoImage
            self.screenshotPhotoView.isHidden = false
        }
    }
    
    //4 Take the screenshot
    let bounds = UIScreen.main.bounds
    UIGraphicsBeginImageContextWithOptions(bounds.size, true, 0.0)
    self.view.drawHierarchy(in: bounds, afterScreenUpdates: true)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    
    //5 show all UI again that you didn't want on your screenshot
    self.showButtonsForScreenshot()
    //6 Now hide the screenshotPhotoView again
    self.screenshotPhotoView.isHidden = true
    self.screenshotPhotoView.image = nil
    return image
}

Solution 5 - Ios

If you want to take screenshot of current screen just call following method on any action event which give you Image object.

-(UIImage *) screenshot
{
    UIGraphicsBeginImageContext(self.view.bounds.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *sourceImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //now we will position the image, X/Y away from top left corner to get the portion we want
    UIGraphicsBeginImageContext(sourceImage.size);
    [sourceImage drawAtPoint:CGPointMake(0, 0)];
    UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    //To write image on divice.
    //UIImageWriteToSavedPhotosAlbum(croppedImage,nil, nil, nil);
    
    return croppedImage;
}

Hope this will help you.

Solution 6 - Ios

CGRect grabRect = CGRectMake(0,0,320,568);// size you want to take screenshot of. 

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
   UIGraphicsBeginImageContextWithOptions(grabRect.size, NO, [UIScreen mainScreen].scale);
 } else {
      UIGraphicsBeginImageContext(grabRect.size);
}
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, -grabRect.origin.x, -grabRect.origin.y);
[self.view.layer renderInContext:ctx];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvodkhangView Question on Stackoverflow
Solution 1 - IosBob de GraafView Answer on Stackoverflow
Solution 2 - IosN SView Answer on Stackoverflow
Solution 3 - IosBB9zView Answer on Stackoverflow
Solution 4 - IosMikkel CortnumView Answer on Stackoverflow
Solution 5 - IosiO.S-C.View Answer on Stackoverflow
Solution 6 - IosnutzView Answer on Stackoverflow