iphone - 如何在iOS5中使用CIColorMatrix?

标签 iphone objective-c ios5 core-image

我正在尝试了解如何更改 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);

}

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

最佳答案

一个月后……

这是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 教程相同的图像):

Reddish

希望对您有所帮助。

关于iphone - 如何在iOS5中使用CIColorMatrix?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9472740/

相关文章:

iphone - Objective C 中的部分模糊 UIImage

iphone - 从数组填充表格仅显示在第一个单元格上

iphone - 在 UITextView 上使用 NSLayoutConstraint 会将 contentSize 重置为 {0,0}

ios5 - iOS应用程序崩溃,没有错误,仅(lldb)

ios - 类似 prepareForSegue 的东西,但用于返回时

iphone - AFNetworking+UIImage 先下载低分辨率图片

iphone - iPad 自定义键盘 GUI

ios - 我已经为聊天应用程序编写了代码,适用于 iOS 12,但在 iOS 13 中,它返回 nil 值

ios - 通过 POST 方法发送带有其他参数的图像

ios - 在iOS aarch64应用程序中使用内联汇编器