1

I am trying to draw an image on the screen via the opengl es 2.0 template. it shows the following errors in the (void)drawRect:(CGRect)rect from MyGLKView. Any ideas to solve this? Thank you!!

CGContextSaveGState: invalid context 0x0
CGContextSetBlendMode: invalid context 0x0
CGContextSetAlpha: invalid context 0x0
CGContextTranslateCTM: invalid context 0x0
CGContextScaleCTM: invalid context 0x0
CGContextDrawImage: invalid context 0x0
CGContextRestoreGState: invalid context 0x0

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

#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
ATNASGDWNGTH
  • 876
  • 11
  • 26

1 Answers1

0

The designated initializer for GLKView is -initWithFrame:context:. You should implement that, not -initWithFrame:, and invoke the same on super.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • I changed. But I still have the same problem. See edited post – ATNASGDWNGTH Jun 03 '12 at 08:28
  • 1
    Your `-drawRect:` method is doing "normal" drawing, which expects a Core Graphics context. A `GLKView` draws into an `EAGLContext`. So, you should be doing OpenGL ES drawing operations. – Ken Thomases Jun 03 '12 at 09:12
  • is it possible to draw an image via the OpenGL ES drawing operations? if yes, would you show me how? Is this link(http://stackoverflow.com/questions/4820372/explain-how-opengl-es-background-images-work) the right direction for me to draw an image ? I have been doing huge amount of googling and trying for one day. I still cannot draw. Thank you! – ATNASGDWNGTH Jun 03 '12 at 13:11
  • I haven't done any OpenGL programming, so I can't help you there. It's definitely possible to draw an image, although it will be much more involved than just a single message to a `UIImage`. However, it sounds like you're using OpenGL when you don't actually want OpenGL. Or, at least, I wouldn't start with OpenGL until you have something OpenGL-specific that you want to accomplish. Just subclass from `UIView` instead of `GLKView` and you can draw your image just like you were trying to. – Ken Thomases Jun 03 '12 at 17:48