我已创建此功能以围绕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
}()
如何应用图层蒙版并仍然在屏幕上显示图像。
答案 0 :(得分:2)
我认为图像没有出现在屏幕上,因为你没有用框架初始化UIImageView。请尝试以下代码段:
let iv = UIImageView(frame: CGRect(x: 10, y: 10, width: 100, height: 100))