无法在iOS5中从CIImage创buildUIImage

我正在使用AVFoundation框架。 在我的示例缓冲区委托我有以下代码:

-(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]; } 

我能够使用CIImage运行人脸检测器等,但它不显示在UIImageView … imageView保持白色。 关于这个问题的任何想法? 我正在使用以下来设置我的会话:

  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]; 

这可能有帮助。 我有同样的问题(图像不被绘制到屏幕)与此代码:

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

但是,将代码更改为此后,现在可以正常工作:

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

要了解更多关于CIContext的信息,请看这里: http : //developer.apple.com/library/ios/#DOCUMENTATION/GraphicsImaging/Reference/QuartzCoreFramework/Classes/CIContext_Class/Reference/Reference.html#//apple_ref/occ/cl / CIContext

这是我得到它的一个方法:

 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); 

注意:多次创build上下文非常糟糕,实际上会导致内存泄漏。 你只需要在你的类实例中创build上下文一次,然后重用它!

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

这是我在项目中使用的完整代码示例。

 - (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基本上是一个图像的配方。 如果您尝试直接从CIImage转换为UIImage则会遇到问题。 例如,打电话

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

将返回nil