2011-12-15 83 views
5

我正在嘗試使用Core Image處理圖像。我已經創建了UIImage類別來完成它。iOS - 無法使用CIFilter處理圖像

我已經加入QuartzCoreCoreImage框架來推算,進口CoreImage/CoreImage.h和使用此代碼:

CIImage *inputImage = self.CIImage; 
CIFilter *exposureAdjustmentFilter = [CIFilter filterWithName:@"CIExposureAdjust"]; 
[exposureAdjustmentFilter setDefaults]; 
[exposureAdjustmentFilter setValue:inputImage forKey:@"inputImage"]; 
[exposureAdjustmentFilter setValue:[NSNumber numberWithFloat:5.0f] forKey:@"inputEV"]; 
CIImage *outputImage = [exposureAdjustmentFilter valueForKey:@"outputImage"]; 
CIContext *myContext = [CIContext contextWithOptions:nil]; 

return [UIImage imageWithCGImage:[myContext createCGImage:outputImage fromRect:outputImage.extent]]; 

但我已得到過濾輸出圖像。

我也試過用CIHueAdjust得到了同樣的結果。

比你提前

更新:我已經找到了解決方案。這是必要的Alloc新CIImage,不僅通過參考UIImage.CIImage這樣:

CIImage *inputImage = [[CIImage alloc] initWithImage:self]; 
+0

喜色調過濾器是爲你工作..什麼是字典鍵需要設置... – 2012-08-30 09:08:13

回答

5

試試下面的代碼: -

CIImage *inputImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"old-country-rain.jpg"]]; 
CIFilter * controlsFilter = [CIFilter filterWithName:@"CIExposureAdjust"]; 
[controlsFilter setValue:inputImage forKey:kCIInputImageKey]; 
[controlsFilter setValue:[NSNumber numberWithFloat: 2.0f] forKey:@"inputEV"]; 
NSLog(@"%@",controlsFilter.attributes); 
CIImage *displayImage = controlsFilter.outputImage; 
UIImage *finalImage = [UIImage imageWithCIImage:displayImage]; 

CIContext *context = [CIContext contextWithOptions:nil]; 
if (displayImage == nil || finalImage == nil) { 
    // We did not get output image. Let's display the original image itself. 
    imageView.image = [UIImage imageNamed:@"old-country-rain.jpg"]; 
}else { 
    // We got output image. Display it. 
    imageView.image = [UIImage imageWithCGImage:[context createCGImage:displayImage fromRect:displayImage.extent]]; 
} 
+3

謝謝!有必要以這種方式分配新的輸入** CIImage **:`CIImage * inputImage = [[CIImage alloc] initWithImage:self];` – 2011-12-15 11:53:57