swift2 具有多重效果的Swift UIView

js81xvg6  于 2022-11-06  发布在  Swift
关注(0)|答案(3)|浏览(179)

几个小时以来,我一直在努力实现以下目标:

我希望在背景中有一个UIImage,最好是一个UIView,背景颜色是红色,带有某种颜色的多重效果(红色区域)。这可能吗?我见过一些UIImage的扩展,可以给它们着色,但是只有当我希望整个图像有红色的多重颜色效果时,这才能起作用。
谢谢

11dmarpk

11dmarpk1#

您可以在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)

使用乘法:

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()
zaq34kh6

zaq34kh62#

Swift 3扩展名(thx到@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
    }
}

示例:

let image = UIImage.multiply(image: sourceImage, color: UIColor.red)
ltskdhd1

ltskdhd13#

借助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
    }
}

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

UIImage(named: "trees")?.tinted(.red, percentageFromBottom: 0.33)

结果是:

相关问题