4

I have a CIImage which is the result/output image of a CIFilter. Now I want to create a texture of that image using GLKTextureLoader. I am using the following function which requires a CGImageRef to achieve that.

[GLKTextureLoader textureWithCGImage: options: error:];

So the question is, what is the best way to convert a CIImage to CGImageRef which could be used by GLKTextureLoader?

I tried the following method but it failed:

CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef ref = [context createCGImage:fullScreenCI fromRect:fullScreenCI.extent];
self.info = [GLKTextureLoader textureWithCGImage:ref options:nil error:&error];

fullscreenCI is the outputImage of a CIFilter.

The above method gives the following error:

error in creating GLK texture : Error Domain=GLKTextureLoaderErrorDomain Code=12 "The operation couldn’t be completed. (GLKTextureLoaderErrorDomain error 12.)" UserInfo=0x1438a880 {GLKTextureLoaderErrorKey=Image decoding failed}

I figured out it could be because of some colorspace issue and tried the following method:

CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef ref = [context createCGImage:fullScreenCI fromRect:fullScreenCI.extent];
fullscreen = [UIImage imageWithData:UIImagePNGRepresentation([UIImage imageWithCGImage:ref])];
self.info = [GLKTextureLoader textureWithCGImage:fullscreen.CGImage options:nil error:&error];

Now the above method works fine! But I want to know if there is any better way to convert the CIImage to CGImageRef without having to use UIImagePNGRepresentation.

4

0 回答 0