GLPaint Sample: how to create the view programmatically?
I need to draw something on an image, like in the GLPaint sample from Apple, but I need to create the EAGL surface where render the OpenGL programmatically.
In the sample is instatiated in the MainWindow.xib
If I try to create the view programmatically with something like:
self.drawingView = [[PaintingView alloc] initWithCoder:nil];
drawingView.frame = CGRectMake(0, 0, 320, 380);
[self.vie开发者_运维知识库w addSubview:drawingView];
I got this error:
failed to make complete framebuffer object 8cd6
Maybe something related to the init? Any hints? Thanks.
From a quick glance inside the source, Apple have put all of the significant initialisation stuff for PaintingView inside initWithCoder:
, from lines 77 to 168. That's part of the NSCoding protocol, which is used for archiving and unarchiving objects from files. If you call initWithCoder:nil
, quite probably the UIView implementation of initWithCoder isn't able to get some relevant value that it needs. At a guess, it probably starts being size zero, which isn't a valid size for a framebuffer object.
I'd suggest you replace initWithCoder:(NSCoder *)coder
with initWithFrame:(CGRect)frame
, calling the same on super at line 86. For an even better implementation, implement both initWithFrame: and initWithCoder:, having the two call a common section for everything enclosed in the if((self = [super initWithXXX:argument]))
block.
精彩评论