Swift UIView具有乘法效果

Gus*_*nér 1 xcode uiimageview ios swift swift2

我几个小时试图达到的目标如下: 在此输入图像描述

我想UIImage在背景中有一个,然后最好是UIView背景颜色为红色,带有某种倍增效果(红色区域).这可能吗?我已经看到UIImage的一些扩展为它们着色,但这只有在我希望我的整个图像具有红色乘法色效果时才有效.

谢谢

Kex*_*Kex 9

你可以在UIImageView的顶部添加一个红色的UIView.调整alpha以使其透明:

let someView = UIView(frame: someImageView.frame)
someView.backgroundColor = UIColor(colorLiteralRed: 255.0/255.0, green: 0, blue: 0, alpha: 0.5)
someImageView.addSubview(someView)
Run Code Online (Sandbox Code Playgroud)

使用乘法代替:

let img = UIImage(named: “background”)
let img2 = UIImage(named: “effect”) //Make sure this is your red image same size as the background


let rect = CGRect(x: 0, y: 0, width: img.size.width, height: img.size.height)

UIGraphicsBeginImageContextWithOptions(img.size, true, 0)
let context = UIGraphicsGetCurrentContext()

// fill the background with white so that translucent colors get lighter
CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor)
CGContextFillRect(context, rect)

img.drawInRect(rect, blendMode: .Normal, alpha: 1)
img2.drawInRect(rect, blendMode: .Multiply, alpha: 1)

// grab the finished image and return it
let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Run Code Online (Sandbox Code Playgroud)


Pet*_*inz 5

Swift 3扩展(感谢@Kex):

extension UIImage{
    class func multiply(image:UIImage, color:UIColor) -> UIImage? {
        let rect = CGRect(origin: .zero, size: image.size)

        //image colored
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
        color.setFill()
        UIRectFill(rect)
        let coloredImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        //image multiply
        UIGraphicsBeginImageContextWithOptions(image.size, true, 0)
        let context = UIGraphicsGetCurrentContext()

        // fill the background with white so that translucent colors get lighter
        context!.setFillColor(UIColor.white.cgColor)
        context!.fill(rect)

        image.draw(in: rect, blendMode: .normal, alpha: 1)
        coloredImage?.draw(in: rect, blendMode: .multiply, alpha: 1)

        let result = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return result
    }
}
Run Code Online (Sandbox Code Playgroud)

例子:

let image = UIImage.multiply(image: sourceImage, color: UIColor.red)
Run Code Online (Sandbox Code Playgroud)


joe*_*ern 5

使用iOS10,您现在UIGraphicsImageRenderer可以像这样简单地添加部分乘法效果:

extension UIImage {
    func tinted(_ color: UIColor, percentageFromBottom: CGFloat) -> UIImage? {
        let imageRect = CGRect(origin: .zero, size: size)
        let colorRect = CGRect(x: 0, y: (1.0 - percentageFromBottom) * size.height, width: size.width, height: percentageFromBottom * size.height)
        let renderer = UIGraphicsImageRenderer(size: size)

        let tintedImage = renderer.image { context in
            color.set()
            context.fill(colorRect)
            draw(in: imageRect, blendMode: .multiply, alpha: 1)
        }

        return tintedImage
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以像这样使用它,将原始值的下三分之一与红色乘法相乘:

UIImage(named: "trees")?.tinted(.red, percentageFromBottom: 0.33)
Run Code Online (Sandbox Code Playgroud)

结果是这样的:

在此输入图像描述