2013-02-25 46 views
0

我使用AVCaptureVideoDataOutput及其委託方法來操縱視頻幀。在委託方法中,我使用sampleBuffer來創建CIImage,從這裏我裁剪CIImage,將其轉換爲UIImage並顯示它。不幸的是,我需要確定這個新的文件大小UIImage,但它返回0。代碼工作,圖像剪裁精美,一切。我只是不明白爲什麼它沒有數據!從CIImage的UIImage - 數據長度爲零?

爲什麼會這樣呢?相關的代碼如下:

//In delegate method, given sampleBuffer... 
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(kCFAllocatorDefault, 
            sampleBuffer, kCMAttachmentMode_ShouldPropagate); 
CIImage *ciImage = [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer 
                options:(NSDictionary *)attachments]; 

... 


dispatch_async(dispatch_get_main_queue(), ^(void) { 
    CGRect rect = [self drawFaceBoxesForFeatures:features forVideoBox:clap 
               orientation:curDeviceOrientation]; 

    CIImage *cropped = [ciImage imageByCroppingToRect:rect]; 
    UIImage *image = [[UIImage alloc] initWithCIImage:cropped]; 

    NSData *data = UIImageJPEGRepresentation(image, 1); 
    NSLog(@"Image size is %d", data.length); //returns 0??? 

    [imageView setImage:image]; 

    [image release]; 
}); 
+0

只是一個NB這兩條線 - 'data.length'返回一個unsigned int,那麼你的'NSLog'說法應該是這樣的,而不是:'的NSLog (@「圖像大小爲%u」,data.length)' – lxt 2013-02-25 23:42:45

+0

這是事實 - 儘管它對輸出沒有影響。我試圖篩選Apple的文檔,僅僅是因爲我知道CoreImage實際上不是圖像,只是數據表示。但在這一點上它超出了我爲什麼轉換時不會有數據。 – bgoers 2013-02-25 23:47:55

+0

是的,我害怕我難倒你的實際問題 - 假設當你在調用'UIImageJPEGRepresentation'後斷點時你的'NSData'對象實際上有數據。 – lxt 2013-02-25 23:49:39

回答

2

我有同樣的問題,但與簡單的過濾圖像。

我偶然發現了this,它解決了這個問題。在此之後,我能夠保存我的形象。

CGSize size = self.originalImage.size; 
CGRect rect; 
rect.origin = CGPointZero; 
rect.size = size; 

UIGraphicsBeginImageContext(size); 
[[UIImage imageWithCIImage:self.filteredImage] drawInRect:rect]; 
UIImage * image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

NSData * jpegData = UIImageJPEGRepresentation(image, 1.0); 

但我只需要在 「ImageContext」