现在我正在开发针对 iOS4.1 或更高版本的图像处理应用程序。
我想在 iOS5 上运行应用程序时使用 CVOpenGLESTextureCache。
我通过以下代码创建纹理缓存
NSDictionary *empty = [NSDictionary dictionary];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], kCVPixelBufferCGImageCompatibilityKey,
[NSNumber numberWithBool:NO], kCVPixelBufferCGBitmapContextCompatibilityKey,
empty, kCVPixelBufferIOSurfacePropertiesKey,
nil];
CVPixelBufferCreate(kCFAllocatorDefault, texSize_.width, texSize_.height, kCVPixelFormatType_32BGRA, (CFDictionaryRef)options, &renderTarget);
CVOpenGLESTextureCacheCreateTextureFromImage (kCFAllocatorDefault,
filterTextureCache, renderTarget,
NULL, // texture attributes
GL_TEXTURE_2D,
GL_RGBA, // opengl format
(int)texSize_.width,
(int)texSize_.height,
GL_BGRA, // native iOS format
GL_UNSIGNED_BYTE,
0,
&renderTexture);
texID_ = CVOpenGLESTextureGetName(renderTexture);
并通过标准方法(glFrameBufferTexture2D 和 glDrawArray)将一些元素渲染到创建的纹理。
之后,我尝试读取 CVPixelBuffer 并创建图像。
- (UIImage *)imageFromTextureCache {
if (renderTarget) {
if (kCVReturnSuccess == CVPixelBufferLockBaseAddress(renderTarget,
kCVPixelBufferLock_ReadOnly)) {
uint8_t* pixels=(uint8_t*)CVPixelBufferGetBaseAddress(renderTarget);
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, pixels, CVPixelBufferGetDataSize(renderTarget), bufferFree);
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGImageRef imageRef = CGImageCreate(texSize_.width, texSize_.height, 8, 32, 4 * texSize_.width, colorSpaceRef, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst, provider, NULL, NO, kCGRenderingIntentDefault);
CGImageRef cloppedImageRef = imageRef;
if (!CGSizeEqualToSize(CGSizeMake(1.0, 1.0), contentRatio_)) {
cloppedImageRef = CGImageCreateWithImageInRect(imageRef, CGRectMake(0, 0, texSize_.width * contentRatio_.width, texSize_.height * contentRatio_.height));
}
UIImage *image = [UIImage imageWithCGImage:cloppedImageRef];
if (!CGSizeEqualToSize(CGSizeMake(1.0, 1.0), contentRatio_)) {
CGImageRelease(cloppedImageRef);
}
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(provider);
CVPixelBufferUnlockBaseAddress(renderTarget, kCVPixelBufferLock_ReadOnly);
return image;
}
return nil;
}
return nil;
}
和
UIImage *image = [outputTexture_ imageFromTextureCache];
然后我得到了以前的渲染图像。不是当前的。
但我修改了如下代码以获得当前渲染图像。
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texID, 0);
GLubyte *buffer = (GLubyte *)calloc(sizeof(GLubyte), 1);
glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
UIImage *image = [outputTexture_ imageFromTextureCache];
我不明白会发生什么。而且我找不到任何有关这方面的文件。
有人帮助我吗?
对不起我的廉价英语......谢谢。