2011-10-17 26 views
16

我正在使用AVFoundation框架。在我的样本缓冲委托我有以下代码:无法在iOS5中从CIImage创建UIImage

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

回答

32

这可能会有帮助。我也有同样的问题(图像不被吸引到屏幕),此代码:

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

+0

谢谢 - 这确实让我画出图像。但是它会泄漏内存,导致应用程序在运行几秒后崩溃。 – user491880

+0

是的,我还没有真的看太多。我相信你必须在某个时候释放上下文或者CGImageRef。另外我敢肯定上面的方法不是使用CIImage视图的实际方法。 我计划明天观看“在iOS和Mac OS X上使用Core Image”视频(该视频位于Apple开发人员网站上的WWDC 2011会话视频中)并深入了解,如果我学习了一些内容任何有用的。 – spybart

+0

我建议你也看这个视频和/或看演示文稿幻灯片,因为它解释了如何使用核心图像,并详细了解上下文以及何时使用CPU/GPU和更多 – spybart

7

这里是我得到它的工作的一种方法:

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

注意:多次创建上下文非常糟糕,实际上会导致内存泄漏。您应该只将该上下文创建为类实例中的一个属性并重用它!

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

没有为我工作。我以不同于此处显示的方式创建了CIImage。 – Automatico

3

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

- (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

+0

辉煌,谢谢!我想知道为什么苹果提供[UIImage imageWithCIImage],如果它很少工作。谢谢苹果。 – SpaceDog

+0

我遇到了一些非常微妙的颜色失真和alpha混合问题,通过设置颜色空间context = [CIContext contextWithOptions:@ {kCIContextWorkingColorSpace:[NSNull null]}]; – user1055568