色调图像(必须有一个更简单的方法)

时间:2014-08-21 07:45:15

标签: ios objective-c colors uiimage core-graphics

我已经研究了人们用来为图像着色的各种方式(我想要应用红色图层),而我唯一一个去上班的方法就是精心制作。有更简单的方法吗?

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

1 个答案:

答案 0 :(得分:1)

如果图像是静态的(在应用程序运行时不会改变),最简单的方法是存储另一个图像并加载它。

如果它不是静态的 - 您正确地执行了此操作。我能想到的另一种方法就是存储一个半透明的红色图像并将其显示在你的图像上。