iPhone UIView以编程方式替换图像颜色,如在Photoshop或Illustrator中 [英] iPhone UIView replace image color programmatically, like in Photoshop or Illustrator

查看:135
本文介绍了iPhone UIView以编程方式替换图像颜色,如在Photoshop或Illustrator中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用中重复使用一组图形。它们是具有透明度的按钮和图像。我不想拥有数十个硬编码按钮图像,而是希望拥有一个单一按钮图像,并能够动态地为其指定颜色,如导航栏和它的色彩。

I have a set of graphics that I would like to reuse within my apps. They are buttons and images with transparency. Rather than having dozens of hard-coded button images, I would love to have a single button image, and be able to assign a color to it dynamically, like the navigation bar and it's tint color.

是否可以替换/转换UIView的色调?

或者:
我知道如果我将透明图像覆盖在另一个UIView上,那么我可以改变背景颜色,两个图像将混合在一起。但透明区域将呈现背景的色调。

Alternatively: I know that if I overlay a transparent image over another UIView, then I can change the background color and the two images will blend together. But the transparent regions will take on the hue of the background.

有没有办法让透明区域保持透明,同时将两个图像混合在一起?我听说有剪贴蒙版,但从来没有工作过跟他们。

Is there a way to keep transparent regions transparent, while blending the two images together? I heard there are clipping masks, but have never worked with them.

谢谢!

更新:

这应该工作,但返回零图像,我见过其他人报告相同的问题。也许这将在未来发挥作用

This should work, but returns nil image, I've seen someone else report the same issue. Maybe this will work in the future

-(void)doHueAdjustFilter
{

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

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

    myFilterAttributes = [myFilter attributes];
    [myFilterAttributes setValue:inputImage forKey:@"inputImage"];
    [myFilterAttributes setValue:[NSNumber numberWithFloat:0.9] forKey:@"inputAngle"];


    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];
    [caduceusImageView setImage:uimage];
    CGImageRelease(cgimg);

}


推荐答案

我'我非常兴奋这段代码。它完全改变了图形或按钮的色调。矢量图形看起来像我在插画中改变了色调。这节省了大量时间,因为我不再需要放弃我的工作,切换到插图画家并开始绘制具有不同色调的按钮。 一个按钮适用于所有人!

I'm super excited about this code snippet. It completely changes the hue of graphics or buttons. Vector graphics look like I changed hue in illustrator. This saves great amounts of time, because I no longer need to drop my work, switch to illustrator and start drawing buttons with different hues. One button works for all!

适用于iOS5

-(UIImage*)doHueAdjustFilterWithBaseImageName:(NSString*)baseImageName hueAdjust:(CGFloat)hueAdjust
{

    CIImage *inputImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:baseImageName]];
    CIFilter * controlsFilter = [CIFilter filterWithName:@"CIHueAdjust"];
    [controlsFilter setValue:inputImage forKey:kCIInputImageKey];

    [controlsFilter setValue:[NSNumber numberWithFloat:hueAdjust] forKey:@"inputAngle"];

    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.
       return  [UIImage imageNamed:baseImageName];
    }else {
        // We got output image. Display it.
        return  [UIImage imageWithCGImage:[context createCGImage:displayImage fromRect:displayImage.extent]];
    }


}

这篇关于iPhone UIView以编程方式替换图像颜色,如在Photoshop或Illustrator中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆