ios - 具有倍增效果的 Swift UIView

标签 ios xcode swift uiimageview swift2

几个小时以来我一直在努力实现的目标如下: enter image description here

我想在背景中有一个 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/

相关文章:

ios - 试图获取对容器 View Controller 的引用

ios - Swift 4 @escaping 类型作为参数

ios - 如何定期更新文本字段?

xcode - 加载 View 导致程序崩溃

swift - 从特定点缩放 SpriteNode

ios - 单击按钮获取数组元素

objective-c - 禁止 UIPickerView 加载 - Obj C

ios - 在代码中动态更改详细信息披露按钮的颜色

Swift,从数组中连续播放多个动画?

ios - 当用户触摸图像时切换图像