【问题标题】:iphone MPMoviePlayerViewController CGContext Errorsiphone MPMoviePlayerViewController CGContext 错误
【发布时间】:2012-11-02 21:33:22
【问题描述】:

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

.h 文件:

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

-(IBAction) playMovie:(id)sender;

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

当我运行程序时,电影将播放,但是当代码执行以下行时: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

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

【问题讨论】:

  • 对问题的描述非常好。

标签: iphone xcode cgcontext


【解决方案1】:

我遇到了同样的问题。就这么说吧:

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

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

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

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

这很愚蠢,但它确实有效。

【讨论】:

  • 这行得通。尽管您能否首先解释一下为什么会出现问题以及为什么该解决方案有效。在我的 ipad 6.1 模拟器中,不会出现该问题;它只发生在我的 iphone 6.1 模拟器中。
  • 谢谢!真的救了我,我开始为这个问题发疯了。
  • 我还必须为“所有异常”禁用我的通用断点,因为这个问题会导致调试器断点停止 30 多次。禁用该断点并修复上面的 graphicsContext 后,模拟器/调试器没有任何问题。
  • @ObjectiveTC 我同意。问题是“所有异常”可以在异常上停止,即使它们被框架捕获 - 它们并不是真正的问题,并且不会在野外使您的应用程序崩溃,但它们在 Xcode 中运行时充当断点。 - 但是,这与上述问题无关;这真的一个问题。这也不例外。这是日志中出现的一堆错误消息。
  • @Till 毫无意义。它在 iOS 7 中已修复,他们不太可能回去修复 iOS 6。
【解决方案2】:

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

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

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

【讨论】:

  • 这是一个非常聪明的解决方案。请参阅下面的修改。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-22
  • 2011-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多