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.

Anand
  • 115
  • 1
  • 12
  • did you try the `[CIContext createCGImage:fromRect:format:colorSpace:]` method? – Jonathan Cichon Jul 26 '13 at 07:21
  • @JonathanCichon Could you please tell me what parameters should I pass for 'format' and 'colorSpace'? I tried passing the constant 'kCIFormatARGB8' for format and CGColorSpaceCreateDeviceRGB() for colorSpace. But got the same error which I got when I used [CIContext createCGImage:(CIImage *) fromRect:(CGRect)]. – Anand Jul 29 '13 at 05:54
  • I see the exact same problem. I'll do more research, but I think this is a bug. – Satoshi Nakajima Oct 09 '13 at 16:08

0 Answers0