带有乘法效果的Swift UIView [英] Swift UIView with multiply effect
问题描述
几个小时以来,我一直在努力实现以下目标:
What I've been trying to achieve for a couple of hours is something like the following:
我想在背景中有一个UIImage
,然后最好是一个背景红色为UIView
的红色,并具有某种乘法效果(红色区域).这可能吗?我已经看到了UIImage的一些扩展对其进行着色,但是只有当我希望我的WHOLE图像具有红色的多色效果时,该扩展才有用.
I would like to have a UIImage
in the background and then preferably a UIView
with a background color of red with some kind of multiply effect (the red area). Is this possible? I've seen a few extensions for UIImage that tints them, but that would only work if I wanted my WHOLE image to have a red multiply color effect.
谢谢
推荐答案
您可以在UIImageView
顶部添加红色UIView
.调整Alpha使其透明:
You could just add a red UIView
to the top of your UIImageView
. Adjust the alpha to make it transparent:
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()
这篇关于带有乘法效果的Swift UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!