iPhone UIView replace image color programmatically

2019-04-11 17:16发布

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条回答
我想做一个坏孩纸
2楼-- · 2019-04-11 17:52

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


}
查看更多
登录 后发表回答