Having trouble creating UIImage from CIImage in iOS5 Having trouble creating UIImage from CIImage in iOS5 ios ios

Having trouble creating UIImage from CIImage in iOS5


This might help. I was having the same issue (image not being drawn to screen) with this code:

CIImage *image = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"mushroom.jpg"]];theImageView.image = [UIImage imageWithCIImage:image];

However, after changing the code to this, it now works correctly:

CIImage *image = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"mushroom.jpg"]];CIContext *context = [CIContext contextWithOptions:nil];theImageView.image = [UIImage imageWithCGImage:[context createCGImage:image fromRect:image.extent]];

To read more about CIContext take a look here:http://developer.apple.com/library/ios/#DOCUMENTATION/GraphicsImaging/Reference/QuartzCoreFramework/Classes/CIContext_Class/Reference/Reference.html#//apple_ref/occ/cl/CIContext


Here is one way that I got it to work:

CIContext *context = [CIContext contextWithOptions:nil];CGImageRef ref = [context createCGImage:result fromRect:ciImage.extent];self.imgView.image = [UIImage imageWithCGImage:ref scale:1.0 orientation:UIImageOrientationRight];CGImageRelease(ref);

Note: Creating the context multiple times is really bad and actually causes a memory leak. You should just create the context once as a property in your class instance and reuse it!


CIImage *ciImage = [UIImage imageNamed:@"imageName.png"].CIImage;UIImage *uiImage = [[UIImage alloc] initWithCIImage:ciImage];