繁体   English   中英

如何在Swift 3中为图像添加不透明度的黑色

[英]How to put a black color with opacity on an image in Swift 3

我想知道如何在Swift 3中的图像顶部放置一个不透明的黑色矩形。例如:

在此输入图像描述 在此输入图像描述

您可以使用不透明度创建一个黑色的UIView ,并使其与UIImageView大小相同,并将其添加为子视图。 尝试类似的东西:

let tintView = UIView()
tintView.backgroundColor = UIColor(white: 0, alpha: 0.5) //change to your liking
tintView.frame = CGRect(x: 0, y: 0, width: imageView.frame.width, height: imageView.frame.height)

imageView.addSubview(tintView)

imageView =你的UIImageView

func drawRectOn(image: UIImage) -> UIImage {
    UIGraphicsBeginImageContext(image.size)
    image.draw(at: CGPoint.zero)
    let context = UIGraphicsGetCurrentContext()

    // set fill gray and alpha
    context!.setFillColor(gray: 0, alpha: 0.5)
    context!.move(to: CGPoint(x: 0, y: 0))
    context!.fill(CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
    context!.strokePath()
    let resultImage = UIGraphicsGetImageFromCurrentImageContext()

    // end the graphics context
    UIGraphicsEndImageContext()

    return resultImage!

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM