从 UIView 子类中使用 OpenGL 绘制 UIImage

发布于 2024-11-03 04:35:23 字数 1484 浏览 0 评论 0原文

我只是想使用 OpenGL 将 backgroung_pic.png 绘制到整个视图。请帮忙!我是 OpenGL 的菜鸟,我正在尝试制作一个类似 Photoshop 的应用程序,并且正在学习 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];

I am just trying to draw backgroung_pic.png to the entire view using OpenGL. Please help! I am a noob to OpenGL and I'm trying to make a photoshop-ish app and am just learning OpenGL to accomplish drawing on images.

My code so far.

[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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文