Search code examples
iphoneobjective-cimagempmovieplayercontrollerimage-capture

How to take a screenshot from an video playing through MPMediaPlayerController in iPhone?


Can anybody help me in this? I want to get an screenshot from an video playing through MPMediaPlayerController. What i have tried so far is:-

-(void)viewDidLoad
{
NSURL *tempFilePathURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"LMFao" ofType:@"mp4"]];
player = [[MPMoviePlayerController alloc] initWithContentURL:tempFilePathURL];
[player setControlStyle:MPMovieControlStyleFullscreen];
//Place it in subview, else it won’t work
player.view.frame = CGRectMake(0, 0, 320, 400);
[self.view addSubview:player.view];
}

-(IBAction)screenshot
{
CGRect rect = [player.view bounds];
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[player.view.layer renderInContext:context];   
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

imgView = [[UIImageView alloc] initWithFrame:CGRectMake(150, 150, 100, 100)];
[imgView setImage:image];
imgView.layer.borderWidth = 2.0;
[self.view addSubview:imgView];
}

And what i ve got now is this:-

ScreenShot

what is happening now is my player's button are getting captured in ScreenShot but my video's image is not been getting.I'm using this context method to capture my image because i have an overlay on my video,using thumbnail method i cant capture video with overlay but i want to get video's image with overlay.

EDIT: what i want is getting screenshot of both this video and drawing overlay as shown in this image Overlay on Video

But what i m getting is by one method i got only video not overlay in my screenshot that method is thumbnail method,i was doing thumbnailing in that which is given by MPMoviePlayerController and by second method i m getting only overlay not an video in ma screenshot,that method is context method.i'm getting context of rect.Now i thought solution for this may be is k i combine both these images.So help me out by giving suggestion is merging images will work for me or not???

Please help.Thanks :)


Solution

  • U can merge two image from this way.Please have an look from the below code..

    - (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
        MaskView=[[UIView alloc] init];
        UIImageView *image1=[[UIImageView alloc]init];
        UIImageView *image2=[[UIImageView alloc]init];
        MaskView.frame=CGRectMake(0, 0,500,500);    
        image1.frame=CGRectMake(0,0,160, 160);  
        image2.frame=CGRectMake(60,54,40,40);
        image1.image=image;
        image2.image=maskImage;
        [MaskView addSubview:image1];
        [MaskView addSubview:image2];   
    
        UIGraphicsBeginImageContext(CGSizeMake(160,160));
    
        [MaskView.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *finishedPic = UIGraphicsGetImageFromCurrentImageContext(); 
    
        return finishedPic;
    }