2

I am currently making a video collage app. I have one view that contains Avplayerlayer as a sublayer. I need to get a screen shot of the view that contains AvplayerLayer. When I tried to take it it gets the screen shot but for the avplayerlayer(which the video is playing inside) is not in the screenshot, just a black screen. For simulator is perfectly working and showing the layer also but for the real device just a black screen.

I tried all the solutions in StackOverFlow and appleds'developer documentation but nothing worked.

Some solutions that I tried:

swift: How to take screenshot of AVPlayerLayer()

Screenshot for AVPlayer and Video

https://developer.apple.com/documentation/avfoundation/avcapturevideopreviewlayer

As you can see on my code it should work for getting the images from the view but for the avplayerlayer is not working.

- (UIImage *)imageFromView:(UIView *)view
{
   UIGraphicsBeginImageContext(view.frame.size);

    [view drawViewHierarchyInRect:_videoFrame afterScreenUpdates:false];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    NSString *fileExtension = @"png";

    NSData *data;

    Boolean *isOutputJPS = false;

    if (isOutputJPS) {
        data = UIImageJPEGRepresentation(image, 0.5);
        fileExtension = @"jpg";
    }else{
        data = UIImagePNGRepresentation(image);
    }

    UIImage  *rasterizedView = [UIImage imageWithData:data];
    UIGraphicsEndImageContext();
    return rasterizedView;
}

//in the viewController

UIImage *image =  [self imageFromView:recordingView];

I am bit desperate now because there is no any solution for the Avplayerlayer. When I check the images generated in the real device.it just shows me the view but for the simulator it works as I expect.

Rameez
  • 412
  • 1
  • 5
  • 16
Mehmet Baykar
  • 367
  • 3
  • 11

1 Answers1

0

There are many ways to achieve what you want to do. I found using an asset image generator always works, all the time.

- (NSImage *)getImageFromAsset:(AVAsset *)myAsset width:(int)theWidth height:(int)theHeight {

                            Float64 durationSeconds = CMTimeGetSeconds(myAsset.duration);

    /// Change frametimetoget section to your specific needs ///
                            CMTime frametimetoget;
                            if (durationSeconds <= 20) {
                                            frametimetoget = CMTimeMakeWithSeconds(durationSeconds/2, 600);
                            } else {
                                            frametimetoget = CMTimeMakeWithSeconds(10, 600);
                            }

                            AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:myAsset];
                            imageGenerator.appliesPreferredTrackTransform = YES;
                            imageGenerator.maximumSize = CGSizeMake(theWidth, theHeight);
                            NSString *aspect = @"AVAssetImageGeneratorApertureModeEncodedPixels";
                            imageGenerator.apertureMode = aspect;

    /// NSError not handled in this example , you would have to add code ///
                            NSError *error = nil;
                            CMTime actualTime;
                            CGImageRef frameImage = [imageGenerator copyCGImageAtTime:frametimetoget actualTime:&actualTime error:&error];

                            Float64 myImageWidth = CGImageGetWidth(frameImage);
                            Float64 myImageHeight = CGImageGetHeight(frameImage);
                            Float64 ratio = myImageWidth/theWidth;
                            NSSize imageSize ;
                            imageSize.width=myImageWidth/ratio;
                            imageSize.height=myImageHeight/ratio;

    /// You may choose to use CGImage and skip below
    /// Swap out NSImage (Mac OS x) for the ios equivalence
                            NSImage * thumbNail = [[NSImage alloc]initWithCGImage:frameImage size:imageSize];

    /// CGImageRelease is a must to avoid memory leaks
                            CGImageRelease(frameImage);
                            return thumbNail;

}

Paul-J
  • 42
  • 1