我正在尝试使用UIimage在蒙版周围创建边框。当我在代码中创建掩码时,它看起来很好:
@IBOutlet weak var imageToBeMasked: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let mask = UIImageView(image: Image)
imageToBeMasked.mask = mask
}
但是当我尝试创造一个笔划时,似乎根本不使用面具:
@IBOutlet weak var imageToBeMasked: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let mask = UIImageView(image: Image)
// the layer used to mask the image view
let maskLayer = CAShapeLayer()
maskLayer.path = UIBezierPath(rect: mask.bounds).cgPath
imageToBeMasked.layer.mask = maskLayer
// the layer used to draw the border
let strokeLayer = CAShapeLayer()
strokeLayer.frame = imageToBeMasked.bounds
strokeLayer.fillColor = UIColor.clear.cgColor
strokeLayer.path = UIBezierPath(rect: mask.bounds).cgPath
strokeLayer.strokeColor = UIColor.black.cgColor
strokeLayer.lineWidth = 4
imageToBeMasked.layer.addSublayer(strokeLayer)
}
有关如何解决此问题的任何线索?
答案 0 :(得分:0)
CALayer
不受遮罩本身的影响。最快的方法是创建一个略大于原始图像的新图像,将其放在原始图像后面并用颜色着色。
let templateImage = originalImage.withRenderingMode(.alwaysTemplate)
strokeImageView.image = templateImage
strokeImageView.tintColor = .black