我创建了这个函数来圆化 UIImageView
上的特定角:
func roundCorners(corners: [UIRectCorner], imageView: UIImageView) -> CAShapeLayer {
let maskPath = UIBezierPath.init(roundedRect: imageView.bounds, byRoundingCorners:[.topLeft], cornerRadii: CGSize.init(width: 7.0, height: 7.0))
let maskLayer = CAShapeLayer()
maskLayer.frame = imageView.frame
maskLayer.path = maskPath.cgPath
return maskLayer
}
当我像这样将它应用于图像 topLeftImage
时:topLeftImage.layer.mask = roundCorners(corners: [.topLeft], imageView: topLeftImage)
,它使图像消失。这是我声明图像的方式:
let topLeftImage: UIImageView = {
let iv = UIImageView()
iv.image = #imageLiteral(resourceName: "myImage")
iv.layoutIfNeeded()
iv.translatesAutoresizingMaskIntoConstraints = false
iv.contentMode = UIViewContentMode.scaleAspectFill
iv.layer.masksToBounds = true
iv.clipsToBounds = true
return iv
}()
如何在应用图层蒙版的同时让图像仍然显示在屏幕上。
最佳答案
我认为图像没有出现在屏幕上,因为您没有使用框架初始化 UIImageView。试试下面的代码片段:
let iv = UIImageView(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
关于ios - 尝试添加图层蒙版时 UIImage 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46170791/