简体   繁体   中英

Tint an Image (there must be a simpler way)

I have looked at the various ways people are using to tint an image (I want to apply a red layer) and the only one I have go to work is ridiculously elaborate. Is there a simpler way?

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

If the image is static (doesn't change while the application is running) the easiest way would be to have another image stored and just load it.

If it's not static - You're doing it correctly. Another way i can think of is just having a half-transparent red image stored and displaying it over Your image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM