在UIView子类中使用OpenGL绘制UIImage

时间:2011-04-23 17:04:13

标签: iphone objective-c uiview opengl-es uiimage

我只是想使用OpenGL将backgroung_pic.png绘制到整个视图中。请帮忙!我是OpenGL的小伙子,我正在尝试制作一个photoshop-ish应用程序,我只是在学习OpenGL来完成图像绘制。

到目前为止我的代码。

[EAGLContext setCurrentContext:context];

CGContextRef    imageContext;
GLubyte         *imageData;
size_t          width, height;
CGImageRef      backgroundImage;

backgroundImage = [UIImage imageNamed:@"background_pic.png"].CGImage;

// Get the width and height of the image
width = CGImageGetWidth(backgroundImage);
height = CGImageGetHeight(backgroundImage);

// Make sure the image exists
if(backgroundImage) {
    // Allocate  memory needed for the bitmap context
    imageData = (GLubyte *) calloc(width * height * 4, sizeof(GLubyte));
    // Use  the bitmatp creation function provided by the Core Graphics framework. 
    imageContext = CGBitmapContextCreate(imageData, width, height, 8, width * 4, CGImageGetColorSpace(backgroundImage), kCGImageAlphaPremultipliedLast);
    // After you create the context, you can draw the  image to the context.
    CGContextDrawImage(imageContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), backgroundImage);
    // You don't need the context at this point, so you need to release it to avoid memory leaks.
    CGContextRelease(imageContext);

    //I'm thinking the drawing goes in here

    free(imageData);
}

// Display the buffer
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];

0 个答案:

没有答案