changing the color of image in iphone sdk

前端 未结 5 855
花落未央
花落未央 2020-12-29 01:06

I have a image & i want to change the color of that image through programatically.

\"enter

相关标签:
5条回答
  • 2020-12-29 01:22

    CoreImage Color Filters work great for this kind of tasks - I find them slightly more straightforward than using the Core Graphic classes (CG...) : They work by allowing you to adjust the RGB and Alpha characteristics of the image I have been using them to change the white background of a QRCode to colored. RGBA of white is (1,1,1,1) in your case I believe you have to reverse the colour. Just check the CI documentation of Apple, there are a few dozen filters available CIColorMatrix is just one of them.

    CIImage *beginImage = [CIImage imageWithCGImage:image.CGImage];
    
    CIContext *context = [CIContext contextWithOptions:nil];
    
    CIFilter *filtercd = [CIFilter filterWithName:@"CIColorMatrix"  //rgreen
                                    keysAndValues: kCIInputImageKey, beginImage, nil];
    [filtercd setValue:[CIVector vectorWithX:0 Y:1 Z:1 W:0] forKey:@"inputRVector"]; // 5
    [filtercd setValue:[CIVector vectorWithX:1 Y:0 Z:1 W:0] forKey:@"inputGVector"]; // 6
    [filtercd setValue:[CIVector vectorWithX:1 Y:1 Z:0 W:0] forKey:@"inputBVector"]; // 7
    [filtercd setValue:[CIVector vectorWithX:0 Y:0 Z:0 W:1] forKey:@"inputAVector"]; // 8
    [filtercd setValue:[CIVector vectorWithX:1 Y:1 Z:0 W:0] forKey:@"inputBiasVector"]; 
    CIImage *doutputImage = [filtercd outputImage];
    
    
    CGImageRef cgimgd = [context createCGImage:doutputImage fromRect:[doutputImage extent]];
    UIImage *newImgd = [UIImage imageWithCGImage:cgimgd];
    
    filterd.image = newImgd;
    
    CGImageRelease(cgimgd);
    
    0 讨论(0)
  • 2020-12-29 01:29

    UPDATE:

    Use this method...

    -(UIImage *)imageNamed:(NSString *)name withColor:(UIColor *)color {
        // load the image
    
        UIImage *img = [UIImage imageNamed:name];
    
        // begin a new image context, to draw our colored image onto
        UIGraphicsBeginImageContext(img.size);
    
        // get a reference to that context we created
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        // set the fill color
        [color setFill];
    
        // translate/flip the graphics context (for transforming from CG* coords to UI* coords
        CGContextTranslateCTM(context, 0, img.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
    
        // set the blend mode to color burn, and the original image
        CGContextSetBlendMode(context, kCGBlendModeColorBurn);
        CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
        CGContextDrawImage(context, rect, img.CGImage);
    
        // set a mask that matches the shape of the image, then draw (color burn) a colored rectangle
        CGContextClipToMask(context, rect, img.CGImage);
        CGContextAddRect(context, rect);
        CGContextDrawPath(context,kCGPathFill);
    
        // generate a new UIImage from the graphics context we drew onto
        UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        //return the color-burned image
        return coloredImg;
    }
    

    Use it like bellow...

    yourImageView.image = [self imageNamed:@"yourImageName" withColor:[UIColor orangeColor]];
    

    hope this helpful to you.....

    0 讨论(0)
  • 2020-12-29 01:30

    Here is the Swift version:

    extension UIImage {
    
        func colorizeWith(color: UIColor) -> UIImage {
            UIGraphicsBeginImageContext(self.size)
            let context = UIGraphicsGetCurrentContext()
            color.setFill()
            CGContextTranslateCTM(context, 0, self.size.height)
            CGContextScaleCTM(context, 1.0, -1.0)
    
            // set the blend mode to color burn, and the original image
            CGContextSetBlendMode(context, kCGBlendModeNormal);
            let rect = CGRectMake(0, 0, self.size.width, self.size.height);
            CGContextDrawImage(context, rect, self.CGImage);
    
            // set a mask that matches the shape of the image, then draw (color burn) a colored rectangle
            CGContextClipToMask(context, rect, self.CGImage);
            CGContextAddRect(context, rect);
            CGContextDrawPath(context,kCGPathFill);
    
            // generate a new UIImage from the graphics context we drew onto
            let coloredImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
    
            //return the color-burned image
            return coloredImage;
        }
    
    }
    
    0 讨论(0)
  • 2020-12-29 01:32

    As I've answered here iPhone - How do you color an image? in my opinion the best way to colorize an image from iOS 7 is by using

    myImageView.image = [[UIImage imageNamed:@"myImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    

    and then change the tintColor of the imageView or whatever contains the image.

    0 讨论(0)
  • 2020-12-29 01:33

    You can try Ankish Jain's answer, it works for me.

    theImageView.image = [theImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    [theImageView setTintColor:[UIColor redColor]];
    
    0 讨论(0)
提交回复
热议问题