xcode iphone MPMoviePlayerViewController CGContext 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13203336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:00:21  来源:igfitidea点击:

iphone MPMoviePlayerViewController CGContext Errors

iphonexcodecgcontext

提问by Joseph

I am writing an app for the iPhone that will play some movies utilizing MPMoviePlayerViewController. So far I have gotten it to play movies, however it is throwing some errors in the debugger that all start with CGContext. I have wracked my brain in trying to fix it. Here are the details of my code:

我正在为 iPhone 编写一个应用程序,该应用程序将使用MPMoviePlayerViewController. 到目前为止,我已经让它播放电影,但是它在调试器中抛出了一些错误,这些错误都以CGContext. 我在试图修复它时绞尽脑汁。以下是我的代码的详细信息:

.h file:

.h 文件:

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface MovieViewController : UIViewController {
    MPMoviePlayerViewController *playerController;
}

-(IBAction) playMovie:(id)sender;

.m file:

.m 文件:

@interface MovieViewController ()
@end
@implementation

-(IBAction)playMovie:(id)sender {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                     pathForResource:@"moviename" ofType:@"mp4"]];
playerController =  [[MPMoviePlayerViewController alloc]
                     initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playerController];
playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playerController.moviePlayer play];
playerController = nil; 
}

When I run the program the movie will play, however when the code executes the line: playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];the following errors occur:

当我运行程序时,电影将播放,但是当代码执行该行时:playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];出现以下错误:

<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextClipToRect: invalid context 0x0
<Error>: CGContextTranslateCTM: invalid context 0x0
<Error>: CGContextDrawShading: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0

I do know that invalid context 0x0means that those specific variables do not have a value, but I have no idea how to remedy this. Any help would be greatly appreciated.

我知道这invalid context 0x0意味着那些特定变量没有值,但我不知道如何解决这个问题。任何帮助将不胜感激。

回答by matt

I was having the same issue. Merely saying this:

我遇到了同样的问题。简单说一下:

MPMoviePlayerViewController* mpvc = 
    [[MPMoviePlayerViewController alloc] initWithContentURL: m];

was causing these invalid contexterror messages. The view controller's view (the fullscreen player) did appear when presented, and played the movie no problem; the only issue was these error messages, which I didn't want showing up in the console.

导致这些invalid context错误消息。视图控制器的视图(全屏播放器)在呈现时确实出现,并且播放电影没有问题;唯一的问题是这些错误消息,我不想显示在控制台中。

Taking a hint from Norman's solution, I simply wrapped the call in an artificial drawing context, like this:

从 Norman 的解决方案中得到提示,我只是将调用包装在一个人工绘图上下文中,如下所示:

UIGraphicsBeginImageContext(CGSizeMake(1,1));
MPMoviePlayerViewController* mpvc = 
    [[MPMoviePlayerViewController alloc] initWithContentURL: m];
UIGraphicsEndImageContext();

It's silly, but it works.

这很愚蠢,但它有效。

回答by Norman

I think the error messages are a bug in MPMoviePlayerViewController

我认为错误消息是 MPMoviePlayerViewController 中的一个错误

They don't seem to be fatal, although I've made them go away by adding a call to UIGraphicsBeginImageContext(self.view.frame.size); in both readyPlayer() and viewDidLoad().

它们似乎并不致命,尽管我通过添加对 UIGraphicsBeginImageContext(self.view.frame.size); 的调用使它们消失了。在 readyPlayer() 和 viewDidLoad() 中。

I also added a UIGraphicsEndImageContext() to dealloc() to clean up the generated contexts.

我还在 dealloc() 中添加了一个 UIGraphicsEndImageContext() 来清理生成的上下文。