简体   繁体   English

(void)drawRect:(CGRect)rect iOS 5.1 OpenGL ES 2.0中的错误

[英]Errors in (void)drawRect:(CGRect)rect iOS 5.1 OpenGL ES 2.0

I am trying to draw an image on the screen via the opengl es 2.0 template. 我正在尝试通过opengl es 2.0模板在屏幕上绘制图像。 it shows the following errors in the (void)drawRect:(CGRect)rect from MyGLKView. 它在MyGLKView的(void)drawRect:(CGRect)rect中显示以下错误。 Any ideas to solve this? 有什么解决办法吗? Thank you!! 谢谢!!

CGContextSaveGState: invalid context 0x0 CGContextSaveGState:无效上下文0x0
CGContextSetBlendMode: invalid context 0x0 CGContextSetBlendMode:无效的上下文0x0
CGContextSetAlpha: invalid context 0x0 CGContextSetAlpha:无效的上下文0x0
CGContextTranslateCTM: invalid context 0x0 CGContextTranslateCTM:无效上下文0x0
CGContextScaleCTM: invalid context 0x0 CGContextScaleCTM:无效上下文0x0
CGContextDrawImage: invalid context 0x0 CGContextDrawImage:无效上下文0x0
CGContextRestoreGState: invalid context 0x0 CGContextRestoreGState:无效的上下文0x0

MyGLKView.m MyGLKView.m

#import "MyGLKView.h"
@implementation MyGLKView

@synthesize pUIImage;

//- (id)initWithFrame:(CGRect)frame
- (id)initWithFrame:(CGRect)frame context:(EAGLContext *)context
{
    //if (self = [super initWithFrame:frame])
    if (self = [super initWithFrame:frame context:context])
    {
        // Initialization code
        pUIImage = [UIImage imageNamed:@"star-green.png"];
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    [pUIImage drawAtPoint:CGPointMake(0, 0)];
}

@end

ViewController.m ViewController.m

#import "ViewController.h"
#import "MyGLKView.h"

@implementation ViewController

@synthesize pContext;

- (void)viewDidLoad
{
    [super viewDidLoad];
    pContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

    if (!pContext)
    {
        NSLog(@"Failed to create ES context");
    }

    //MyGLKView *pMyGLKView = [[MyGLKView alloc] init];
    //pMyGLKView.context = pContext;
    //self.view = pMyGLKView;
    self.view = [[MyGLKView alloc] initWithFrame:[[UIScreen mainScreen] bounds] context:pContext];
}

- (void)viewDidUnload
{    
    [super viewDidUnload];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc. that aren't in use.
}

- (void)update
{

}

@end

The designated initializer for GLKView is -initWithFrame:context: . GLKView的指定初始值GLKViewGLKView -initWithFrame:context: GLKView You should implement that, not -initWithFrame: , and invoke the same on super . 您应该实现而不是-initWithFrame:并在super上调用它。

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

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