iOS之给图片添加圆角效果和阴影效果(Swift4.2)

给图片添加圆角效果 

    override func viewDidLoad() {
        super.viewDidLoad()

       //给图片添加圆角效果
        let image = UIImage(named:"cat")
        let imageView = UIImageView(image: image)

        let rect = CGRect(x: 10, y: 80, width: 300, height: 300)
        imageView.frame = rect
        imageView.layer.cornerRadius = 150
       imageView.layer.masksToBounds = true //设置图像的遮罩覆盖属性,进行覆盖截切

        imageView.layer.borderColor = UIColor.lightGray.cgColor
        imageView.layer.borderWidth = 10
        self.view.addSubview(imageView)

        
        

}

iOS之给图片添加圆角效果和阴影效果(Swift4.2)_第1张图片

 给图片添加阴影效果:

        //给图像添加阴影效果
        let image = UIImage(named:"cat")
        let imageView = UIImageView(image: image)

        let rect = CGRect(x: 36, y: 80, width: 248, height: 164)
        imageView.frame = rect

        imageView.layer.shadowColor = UIColor.black.cgColor
        imageView.layer.shadowOffset = CGSize(width: 10.0, height: 10.0) //设置阴影的横向和纵向偏移值
        imageView.layer.shadowOpacity = 0.45 //设置阴影透明度
        imageView.layer.shadowRadius = 5.0 //设置图像摄图层的阴影半径大小

        self.view.addSubview(imageView)

iOS之给图片添加圆角效果和阴影效果(Swift4.2)_第2张图片

你可能感兴趣的:(iOS之给图片添加圆角效果和阴影效果(Swift4.2))