1. 程式人生 > >iOS之給圖片新增圓角效果和陰影效果(Swift4.2)

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)

        
        

}

 給圖片新增陰影效果:

        //給影象新增陰影效果
        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)