我几个小时试图达到的目标如下:
我想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()