簡體   English   中英

如何通過iPhone中的MPMediaPlayerController播放視頻截圖?

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

有人可以幫助我嗎? 我想從通過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結合這兩個圖像。所以通過給出建議幫助我是合並圖像將為我工作或不?

請幫忙。謝謝:)

您只需要在mpmovieplayer對象上調用單個方法:

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

此方法將返回UIImage對象。 你要做的只是:

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

有關更多詳細信息,請查看MPMoviePlayerController

希望這可以幫助

編輯:在您的方法中,您可以先隱藏玩家的控制,然后再次顯示控件后捕獲圖像。 你可以使用MPMoviePlayerController的controlStyle屬性來實現這個目的。

不確定這是你在找什么,但這是我所理解的。 如果您正在尋找不同的讓我知道。

你可以通過這種方式合並兩個圖像。請看下​​面的代碼..

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

這可能有所幫助

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

在給定時間獲取thumbnailImage並縮放該圖像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM