iphone – 如何在iOS5中使用CIColorMatrix?

栏目: IOS · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/9472740/how-to-use-cicolormatrix-in-ios5
我试图找出如何改变UI Image

的颜色/色调.我发现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

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

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

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

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

希望能帮助到你.

翻译自:https://stackoverflow.com/questions/9472740/how-to-use-cicolormatrix-in-ios5


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

社群运营的艺术

社群运营的艺术

查尔斯·沃格 / 靳婷婷 / 华夏出版社 / 2017-7 / 42

社群存续的秘密,长期以来只有少数人知道,比如佛陀、耶稣及其弟子。 回溯3000年社群史,《社群运营的艺术》作者查尔斯•沃格总结了有归属感社群的七大原则。 在前互联网时代,七原则曾经造就伟大社群。在人人互联时代,应用七原则的社群将更繁荣。 本书作者耶鲁大学神学硕士查尔斯•沃格研究人类社会3000年的历史,结合个人亲身操作经历,提出了七条历经时间考验的原则:界限原则、入会原则、仪式原......一起来看看 《社群运营的艺术》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

SHA 加密
SHA 加密

SHA 加密工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具