2011-10-17 27 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