为什么CGContextDrawImage没有绘制图像?

时间:2013-09-26 04:00:39

标签: ios objective-c uiview opengl-es quartz-2d

我想通过UIView绘制图像。但它没有显示出来。图像应该是纹理。

这是我的UIView功能

- (void)drawRect:(CGRect)rect{


NSString *path = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"png"];
NSData *texData = [[NSData alloc] initWithContentsOfFile:path];
UIImage *image = [[UIImage alloc] initWithData:texData];
GLuint width = CGImageGetWidth(image.CGImage);
GLuint height = CGImageGetHeight(image.CGImage);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
void *imageData = malloc( height * width * 4 );
CGContextRef context = CGBitmapContextCreate( imageData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );
CGColorSpaceRelease( colorSpace );
CGContextClearRect( context, CGRectMake( 0, 0, width, height ) );
CGContextTranslateCTM( context, 0, height - height );
CGContextDrawImage( context, CGRectMake( 0, 0, width, height ), image.CGImage );


glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);


CGContextRelease(context);


[[UIImage imageWithCGImage:imageData] drawInRect:CGRectMake(0, 0, 145, 15)];

// free(imageData);
[image release];
[texData release];}

0 个答案:

没有答案