【问题标题】:How to use CIColorMatrix in iOS5?如何在 iOS5 中使用 CIColorMatrix?
【发布时间】:2012-03-17 09:09:42
【问题描述】:

我正在尝试了解如何更改 UIImage 的颜色/色调。我发现 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);

}

什么代码进入这个过滤器的字典?

【问题讨论】:

    标签: iphone objective-c ios5 core-image


    【解决方案1】:

    只是一个带有链的 Swift 示例,由 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")
    

    请注意,RGB 的默认值分别为 1,0,0 0,1,0 和 0,0,1,如果您想让它更红,您可以将 inputRVector 设置为 1,1,1 并保留其他相同(因为这将蓝色和绿色分量乘以红色值)

    【讨论】:

      【解决方案2】:

      一个月后...

      这是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,如果你在那里得到 nil,那么请确保你传递了正确的 UIImage/CGImage 或路径。

      2 创建过滤器,你知道的:)

      3 中,将过滤器参数设置为其默认值,CoreImage 编程指南建议我们应该这样做(如果避免的话,我没有尝试过任何奇怪/坏的事情。)

      4中设置输入ciimage

      58 我们设置参数。例如,我制作了红色矢量 {1,1,1,0},因此图像看起来偏红678 在这里没有必要,因为它们的值与默认值相同(记得我们称为 -setDefaults 吗?)但出于教育目的,我想它们很好:)

      9中设置输出图像,虽然还没有绘制出来。

      最后在10 中,您告诉CoreImage 将输出图像绘制成CGImage,然后我们将CGImage 放入UIImage 并将其放入UIImageView。

      这是结果(我使用了与this教程相同的图像):

      希望对你有帮助。

      【讨论】:

      • 非常好的答案。只是#1 注释的旁注。由于图像类型,有时 ciimage 为 nil。所以有时有必要创建 cgimage,然后从中创建一个 ciimage。
      • @Aggressor 你能否举一个图像类型的例子,它将为 ciimage 返回 nil?我想编辑解决它的答案:)
      • 这是一篇解释 3.medium.com/@ranleung/uiimage-vs-ciimage-vs-cgimage-3db9d8b83d94 的文章。如果您正在使用已知类型(例如 .jpg)并且您始终知道您将拥有 CIImage,那么您是安全的。但是,如果您的应用程序采用多种类型的文件格式,那么您应该在 CIImage 为 nil 时采取保护措施,而是抓取 CGImage 并将其转换为 CIImage(这可能意味着在上下文中重新绘制它并从中抓取 UIImage->CIImage )
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-10
      相关资源
      最近更新 更多