iPhone UIView replace image color programmatically, like in Photoshop or Illustrator

怎甘沉沦 提交于 2019-12-06 08:39:30

问题


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.

Is it possible to replace/shift the hue of a 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.

Thank you!

Update:

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);

}

回答1:


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!

Works on 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]];
    }


}


来源:https://stackoverflow.com/questions/9470438/iphone-uiview-replace-image-color-programmatically-like-in-photoshop-or-illustr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!