Having trouble creating UIImage from CIImage in iOS5
Solution 1
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
Solution 2
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!
Solution 3
CIImage *ciImage = [UIImage imageNamed:@"imageName.png"].CIImage;
UIImage *uiImage = [[UIImage alloc] initWithCIImage:ciImage];
Solution 4
Here is a complete code sample that I've used in my projects.
- (UIImage *)makeUIImageFromCIImage:(CIImage *)ciImage {
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgImage = [context createCGImage:ciImage fromRect:[ciImage extent]];
UIImage* uiImage = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
return uiImage;
}
CIImage
is basically a recipe for an image. You will run into problems if you try to directly convert from CIImage
to UIImage
. For example, calling
NSData * UIImageJPEGRepresentation (
UIImage *image,
CGFloat compressionQuality
);
will return nil
.
Related videos on Youtube
user491880
Updated on June 04, 2022Comments
-
user491880 almost 2 years
I'm using the AVFoundation framework. In my sample buffer delegate I have the following code:
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection{ CVPixelBufferRef pb = CMSampleBufferGetImageBuffer(sampleBuffer); CIImage *ciImage = [CIImage imageWithCVPixelBuffer:pb]; self.imageView.image = [UIImage imageWithCIImage:ciImage]; }
I am able to use the CIImage to run the face detector etc. but it does not show up in the UIImageView ... the imageView remains white. Any ideas as to the problem? I am using the following to setup my session:
self.session = [[AVCaptureSession alloc] init]; self.session.sessionPreset = AVCaptureSessionPreset640x480; self.videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; self.videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil]; self.frameOutput = [[AVCaptureVideoDataOutput alloc] init]; self.frameOutput.videoSettings = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
-
Automatico over 8 yearsDid not work for me. I created the CIImage in a different way than shown here.
-
Duck almost 7 yearsbrilliant, thanks!!! I wonder why Apple provides [UIImage imageWithCIImage] if it rarely works. Thanks Apple.
-
user1055568 over 6 yearsI ran into some very subtle color distortion and alpha blending issues with this method, fixed by setting color space context = [CIContext contextWithOptions:@{kCIContextWorkingColorSpace : [NSNull null]}];
-
Airsource Ltd about 5 yearsThanks @user1055568 that was the critical piece of information we needed