【问题标题】:How to take a screenshot from an video playing through MPMediaPlayerController in iPhone?如何从 iPhone 中通过 MPMediaPlayerController 播放的视频中截取屏幕截图?
【发布时间】:2012-05-14 12:03:59
【问题描述】:

有人可以帮我吗?我想从通过 MPMediaPlayerController 播放的视频中获取屏幕截图。到目前为止我尝试过的是:-

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

而我现在得到的是:-

现在发生的情况是我的播放器按钮在 ScreenShot 中被捕获,但我的视频图像没有被捕获。我正在使用此上下文方法来捕获我的图像,因为 我的视频上有一个叠加层,使用缩略图方法我无法捕获带有叠加层的视频,但我想获取带有叠加层的视频图像。

编辑: 我想要的是获取此视频和绘图叠加层的屏幕截图,如图所示

但我得到的是通过一种方法,我在我的屏幕截图中只得到了视频而不是覆盖,该方法是缩略图方法,我正在使用 MPMoviePlayerController 给出的缩略图,而通过第二种方法,我只得到覆盖而不是 ma 中的视频屏幕截图,该方法是上下文方法。我正在获取 rect 的上下文。现在我认为解决方案可能是 ki 将这两个图像结合起来。所以通过给出建议来帮助我合并图像是否适合我???

请帮忙。谢谢:)

【问题讨论】:

  • No 一点也不,我正在使用 MediaPlayer 框架播放视频,我没有用过其他任何东西:)

标签: iphone objective-c image mpmovieplayercontroller image-capture


【解决方案1】:

你只需要在你的 mpmovieplayer 对象上调用一个方法:

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

此方法将返回一个 UIImage 对象。你要做的只是:

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

更多详情请查看MPMoviePlayerController

希望对你有帮助

编辑:在您的方法中,您可以先隐藏播放器的控件,然后再捕获图像,然后再次显示控件。您可以使用 MPMoviePlayerController 的 controlStyle 属性来实现。

不确定这是您要查找的内容,但这是我所理解的。如果您正在寻找不同的东西,请告诉我。

【讨论】:

  • 我更新了我的答案。如果这仍然不是您想要的,请告诉我
  • 嘿伙计,我想做的是在视频上设置叠加层并捕获图像。当我捕获图像时,视频播放器的按钮显示在图像中,但视频的图像未显示,如上面的屏幕截图所示。
  • 你的回答有问题,即我的覆盖没有显示出来。
【解决方案2】:

你可以通过这种方式合并两个图像。请看下​​面的代码..

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

【讨论】:

    【解决方案3】:

    这可能会有所帮助

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

    在给定时间获取 thumbnailImage 并缩放该图像。

    【讨论】:

    • 我已经完成了,但它没用,因为我的视频上有叠加层,使用此缩略图我们只能获取视频的图像而不是叠加层的图像。!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    相关资源
    最近更新 更多