2012-02-27 23 views
6

我想了解如何更改UIImage的顏色/色調。我發現iOS5有很多圖像過濾器,但我很難找到有關正確使用CIColorMatrix過濾器的文檔。如何在iOS5中使用CIColorMatrix?

-(void)doCIColorMatrixFilter 
{ 

    //does not work, returns nil image 
    CIImage* inputImage = [CIImage imageWithCGImage:[[UIImage imageNamed:@"button.jpg"]CGImage]]; 

    CIFilter *myFilter; 
    NSDictionary *myFilterAttributes; 
    myFilter = [CIFilter filterWithName:@"CIColorMatrix"]; 
    [myFilter setDefaults]; 

    myFilterAttributes = [myFilter attributes]; 

    [myFilterAttributes setValue:inputImage forKey:@"inputImage"]; 
    //How to set up attributes? 

    CIContext *context = [CIContext contextWithOptions:nil]; 
    CIImage *ciimage = [myFilter outputImage]; 
    CGImageRef cgimg = [context createCGImage:ciimage fromRect:[ciimage extent]]; 
    UIImage *uimage = [UIImage imageWithCGImage:cgimg scale:1.0f orientation:UIImageOrientationUp]; 
    [imageView setImage:uimage]; 
    CGImageRelease(cgimg); 

} 

什麼代碼進入該過濾器字典?

回答

20

一個月後...

這是CIColorMatrix設置它的所有參數:)

-(void)doCIColorMatrixFilter 
{ 
    // Make the input image recipe 
    CIImage *inputImage = [CIImage imageWithCGImage:[UIImage imageNamed:@"facedetectionpic.jpg"].CGImage]; // 1 

    // Make the filter 
    CIFilter *colorMatrixFilter = [CIFilter filterWithName:@"CIColorMatrix"]; // 2 
    [colorMatrixFilter setDefaults]; // 3 
    [colorMatrixFilter setValue:inputImage forKey:kCIInputImageKey]; // 4 
    [colorMatrixFilter setValue:[CIVector vectorWithX:1 Y:1 Z:1 W:0] forKey:@"inputRVector"]; // 5 
    [colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:1 Z:0 W:0] forKey:@"inputGVector"]; // 6 
    [colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:0 Z:1 W:0] forKey:@"inputBVector"]; // 7 
    [colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:0 Z:0 W:1] forKey:@"inputAVector"]; // 8 

    // Get the output image recipe 
    CIImage *outputImage = [colorMatrixFilter outputImage]; // 9 

    // Create the context and instruct CoreImage to draw the output image recipe into a CGImage 
    CIContext *context = [CIContext contextWithOptions:nil]; 
    CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]]; // 10 

    // Draw the image in screen 
    UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageWithCGImage:cgimg]]; 
    CGRect f = imageView2.frame; 
    f.origin.y = CGRectGetMaxY(imageView.frame); 
    imageView2.frame = f; 

    [self.view addSubview:imageView2]; 
} 

爲例所以這是什麼樣的作用:

1我們創造的ciimage,如果你得到零,那麼確保你傳遞正確的UIImage/CGImage或路徑。

2創建過濾器,你就知道這:)

3設置過濾參數,它的默認值,CoreImage編程指南建議我們應該這樣做,雖然(我還沒有嘗試任何異樣/壞的事情,如果避免。)

4設置輸入ciimage

5通過8我們設置的參數。例如,我製作了紅色矢量{1,1,1,0},因此圖像看起來像是帶紅色678並沒有必要在這裏,因爲它們的值作爲默認值(還記得我們叫-setDefaults?)相同的,但教育目的我想他們都很好:)

9設置輸出圖像,雖然不繪製尚未。

終於在10你告訴CoreImage輸出圖像繪製成CGImage,我們把那個CGImage到的UIImage,它的裏面的UIImageView。

這是結果(我用同樣的圖像this教程):

Reddish

希望它能幫助。

+0

偉大的回答。只有#1音符的旁註。有時由於圖像類型的原因,ciimage是零。所以有時它必須創建cgimage,然後創建一個ciimage。 – Aggressor 2014-12-23 18:45:43

+0

@Aggressor你可以給一個圖像類型的例子,將返回零爲ciimage?我想編輯答案解決它:) – nacho4d 2014-12-25 10:24:40

+0

繼承人一篇文章,解釋了3. https://medium.com/@ranleung/uiimage-vs-ciimage-vs-cgimage-3db9d8b83d94。如果你正在使用已知類型(比如.jpg),並且你總是知道你將擁有CIImage,那麼你就很安全。但是如果你的應用程序採用多種類型的文件格式,那麼當CIImage爲零時應該有保護措施,並且抓取CGImage並將其轉換爲CIImage(這可能意味着在上下文中重新繪製它並從中獲取UIImage-> CIImage ) – Aggressor 2014-12-25 19:04:59

5

只是與鏈一個夫特例如通過nacho4d

var output = CIFilter(name: "CIColorControls") 
output.setValue(ciImage, forKey: kCIInputImageKey) 
output.setValue(1.8, forKey: "inputSaturation") 
output.setValue(0.01, forKey: "inputBrightness") 

filter = CIFilter(name: "CIColorMatrix") 
filter.setDefaults() 
filter.setValue(output.outputImage, forKey: kCIInputImageKey) 
filter.setValue(CIVector(x: 1, y: 0.1, z: 0.1, w: 0), forKey: "inputRVector") 
filter.setValue(CIVector(x: 0, y: 1, z: 0, w: 0), forKey: "inputGVector") 
filter.setValue(CIVector(x: 0, y: 0, z: 1, w: 0), forKey: "inputBVector") 

注添加到上面的回答是,默認值是1,0,0 0,1,0和0,0,1分別用於RGB和如果你想使它變得更紅,你可以將inputRVector設置爲1,1,1,並保持其他值相同(因爲這將紅色值乘以藍色和綠色分量)