简体   繁体   English

如何通过iPhone中的MPMediaPlayerController播放视频截图?

[英]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. 我想从通过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:- 我现在得到的是: -

截图

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. 现在正在发生的事情是我的播放器的按钮被捕获在ScreenShot但我的视频图像没有得到。我正在使用这种上下文方法来捕捉我的图像,因为我在我的视频上有一个叠加,使用缩略图方法我无法捕捉视频与覆盖,但我想获得叠加的视频图像。

EDIT: what i want is getting screenshot of both this video and drawing overlay as shown in this image 编辑:我想要的是获取此视频和绘图叠加的截图,如此图所示 覆盖视频

But what im 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 im 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 ki combine both these images.So help me out by giving suggestion is merging images will work for me or not??? 但是我得到的是一种方法,我只得到视频没有叠加在我的截图中,方法是缩略图方法,我正在进行缩略图,由MPMoviePlayerController给出,第二种方法我只获取叠加而不是ma截图中的视频,方法是上下文方法。我得到rect的上下文。现在我认为解决方案可能是ki结合这两个图像。所以通过给出建议帮助我是合并图像将为我工作或不?

Please help.Thanks :) 请帮忙。谢谢:)

You just have to call a single method on your mpmovieplayer object: 您只需要在mpmovieplayer对象上调用单个方法:

- (UIImage *)thumbnailImageAtTime:(NSTimeInterval)playbackTime timeOption:(MPMovieTimeOption)option

this method will return a UIImage object. 此方法将返回UIImage对象。 what you have to do is just: 你要做的只是:

UIImage *thumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];

for more details look at MPMoviePlayerController 有关更多详细信息,请查看MPMoviePlayerController

Hope this helps 希望这可以帮助

EDIT: In your method you can first hide player's control and than capture Image after that show controls again. 编辑:在您的方法中,您可以先隐藏玩家的控制,然后再次显示控件后捕获图像。 you can achive this using controlStyle property of MPMoviePlayerController. 你可以使用MPMoviePlayerController的controlStyle属性来实现这个目的。

Not sure this is what you are looking for but it is what I understood. 不确定这是你在找什么,但这是我所理解的。 If you are looking for different let me know. 如果您正在寻找不同的让我知道。

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

This may help 这可能有所帮助

  - (UIImage *)thumbnailImageAtTime:(NSTimeInterval)playbackTime timeOption:(MPMovieTimeOption)option

Get the thumbnailImage at the given time and scale that image. 在给定时间获取thumbnailImage并缩放该图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM