我想在背景中有一个 UIImage
,然后最好是一个背景颜色为红色的 UIView
并具有某种乘法效果(红色区域)。这可能吗?我已经看到一些 UIImage 的扩展可以对它们进行着色,但只有在我希望我的整个图像具有红色倍增色效果时才有效。
谢谢
最佳答案
您可以在 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()
关于ios - 具有倍增效果的 Swift UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34483352/