Problems creating UIImage from CIImage in iOS5

I am using the AVFoundation framework. In my fetch buffer representative, 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 can use CIImage to start the face detector, etc., but it does not appear in UIImageView ... imageView remains white. Any ideas regarding the problem? To configure the session, I use the following:

  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]; 
+16
ios iphone ios5 avfoundation core-image
Oct 17 2018-11-11T00:
source share
4 answers

This can help. I had the same problem (the image is not drawn on the screen) using this code:

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

However, after changing the code on it, it 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]]; 

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

+32
Oct 17 2018-11-11T00:
source share

Here is one way to make me 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 context several times is very bad and actually causes a memory leak. You should simply create a context once as a property in an instance of your class and reuse it!

+7
Oct 17 2018-11-17T00:
source share
 CIImage *ciImage = [UIImage imageNamed:@"imageName.png"].CIImage; UIImage *uiImage = [[UIImage alloc] initWithCIImage:ciImage]; 
+4
May 10 '12 at 13:12
source share

Here is a complete sample of the code that I 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 an image recipe. You will have problems if you try to directly convert from CIImage to UIImage . For example, a call

 NSData * UIImageJPEGRepresentation ( UIImage *image, CGFloat compressionQuality ); 

nil will return.

+3
Mar 18 '15 at 13:56
source share



All Articles