1

I have an requirement to capture iPhone screen when my app is in foreground . I have used UIGraphicsGetImageFromCurrentImageContaxt() for this it works in most of synerio but fails when video is playing by using MPMoviePlayerViewController or AVPlayer and gives back black image with player control.

Probabely My guess is MPMoviePlayerViewController rendering frames using OpenGl and method UIGraphicsGetImageFromCurrentImageContaxt() is not able to capture the image ??

I am missing something or is there any alternative soln available to capture iPhone Screen while app is in foreground ??

iOSPawan
  • 2,884
  • 2
  • 25
  • 50

3 Answers3

3

There is not any easy solution(i.e. might be I don't know) for this but you need to figure out. Here I have described possible solutions. When you will try simply rendering the view on context then it will give blank screen in place of player and other things will be as it is.

Possible solutions that I know
Private API
You can use UIGetScreenImage() function to capture whole screen of device including player and its control. This is the way you can get the image of player with view easily.
Note: Some buddies are saying use of this function may cause rejection of application(I have never used for app store's app so I don't have much experience about it :)).

Second way.

If you want to get image of player with the other things that resides in main view or iPhone screen then basic idea is to capture two images(i.e. One of movie player and another of whole screen like you are doing using UIGraphicsGetImageFromCurrentImageContext) and combine them as a one image using some proper calculations.In this way you need to do some calculations so accuracy is depend upon your calculations.

Here I am preferring to use AVPlayerinstead of MPMoviePlayer not sure I am true or not but as I have feel that AVPlayer providing exact frame at certain time(Good accuracy. Exact image that is showing on the screen.)

Please set the gravity mode of AVPlayer to AVLayerVideoGravityResizeAspect. This mode will preserver aspect ratio and fit within layers bounds(i.e. This option is default). You can set this way.

playerLayer.videoGravity = setVideoFillMode:AVLayerVideoGravityResizeAspect;

where playerLayer is the object of AVPlayerLayer.

Now get the image of AVPlayerLayer

AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] 
                                                     initWithAsset:asset];
            imageGenerator.appliesPreferredTrackTransform=YES;
            /*AVAssetImageGenerator will scale images such that they fit within the defined bounding box. Images will never be scaled up. So provide this should be the size of AVPlayer. */
            imageGenerator.maximumSize=CGSizeMake(400, 400); 
            CGImageRef imgRef = [imageGenerator copyCGImageAtTime:storedCMTime actualTime:NULL error:NULL];
            [imageGenerator release];

Please note that this generated image might be not exact size for what you are looking for or as you have exactly provided in maximum size because as I mentioned in above comment here image will be never scaled up. if image is not the exact size for what you are looking then use some image utility function to scale the image as per the size of your player but don't spoil Aspect ration because we have set the AVPLayer's mode to AVLayerVideoGravityResizeAspect.store this image temporary.

Now Capture the image of View using UIGraphicsGetImageFromCurrentImageContext(i.e. like you are doing currently). Draw image of AVplayer that we have captured and stored over exactly the black area that is showing on your view(i.e. You need to do some trial and error to get the exactly same).

Here I have tried to cover all the main points that may cause panic or not immediately obvious.

Update: Screen capturing solutions from APPLE: http://developer.apple.com/library/ios/#qa/qa1703/_index.html#//apple_ref/doc/uid/DTS40010193

If you would like to go with MPMoviewPlayerController then it's okay. Get the thumbnail using code shows by @Kuldeep. And combine the Both view's image and Thumbnail using Image Masking exactly explain here : Link

Community
  • 1
  • 1
Iducool
  • 3,543
  • 2
  • 24
  • 45
  • Thanks bro, AS MPMoviePlayerViewController comes up with default and good looking layout. So I have used MPMoviePlayerViewController. But I found syncing issue in both MPMoviePlayer and AVPlayer. MPMoviePlayerViewController itself show wronn frame while seek in/out. Hope could have be done by UIGraphics api.... anyway thanks for the second approach. – iOSPawan Aug 27 '12 at 16:56
  • So are you not getting correct frame of video? As far as I know there is not any other approach to get the photo of video. When they make the function: UIGetScreenImage() private at that time they suggested to get the frame of video to capture the video. Though look for any solution might be any genius have some workaround. – Iducool Aug 28 '12 at 04:17
0

I would rather suggest to use below snippet, may it would be more easy to use. You just need to pass time factor at which you need to capture the video.

float timeFactor = 60.0;
CGRect rect = CGRectMake(self.view.center.x, self.view.center.y, 100.0, 100.0)
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
UIImage *thumbnail = [player thumbnailImageAtTime:timeFactor timeOption:MPMovieTimeOptionNearestKeyFrame];
UIImageView *imagV=[[UIImageView alloc]initWithImage:thumbnail];
[imagV setFrame:rect];
Kuldeep
  • 2,589
  • 1
  • 18
  • 28
0

Well, it seems I've found a solution to capture frames in a AVPlayer:

    - (UIImage*) captureFrame {

    AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:[self.player.currentItem asset]];

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5) {
        [imageGenerator setRequestedTimeToleranceAfter:kCMTimeZero];
        [imageGenerator setRequestedTimeToleranceBefore:kCMTimeZero];
    }

    CGImageRef ref = [imageGenerator copyCGImageAtTime:self.player.currentItem.currentTime actualTime:nil error:nil];
    UIImage *img = [UIImage imageWithCGImage:ref];
    CGImageRelease(ref);

    return img;
}
Dmitry13
  • 1
  • 1