Search code examples
iphoneipadscreenshotavplayer

iPhone and iPad, take screenshot with video playing


My app need to take some screenshot when a video is being played on the screen. The video is played by AVPlayer and AVPlayerLayer. The problem when I try to use old methods, such as:

UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
CGRect rect = [keyWindow bounds];

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
else
    UIGraphicsBeginImageContext(rect.size);

CGContextRef context = UIGraphicsGetCurrentContext();
[keyWindow.layer renderInContext:context];   
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;

It returns an image of the screen and a black area (which the video should be there). I can take a screenshot successfully with UIGetScreenImage() but it is a private API so I am not sure if I can use it or not


Solution

  • From what I have read, many published apps have been using UIGetScreenImage(). It seems like Apple is quite lenient with its usage. Perhaps you could take a chance...