UIImageView问题上的影子

时间:2017-07-19 10:53:37

标签: ios swift uiimageview uicollectionviewcell

我正在尝试在cornerRadius内设置UIImageVIewUICollectionViewCell的阴影。问题是我设置clipToBounds = true并停止阴影和cornerRadius

我想拥有cornerRadius,clipToBounds和图像的阴影。不知道我在这里做错了什么。

class PostPhotoCell: UICollectionViewCell {

@IBOutlet weak var selectedImage: UIImageView!
@IBOutlet weak var deleteButton: UIButton!


    override func awakeFromNib()
    {
    super.awakeFromNib()

    selectedImage.layer.cornerRadius = 5.0
    selectedImage.contentMode = .scaleAspectFill
    //applying overall shadow to image
    selectedImage.layer.shadowColor = UIColor(white: 0.0, alpha: 0.5).cgColor
    selectedImage.layer.shadowOffset = CGSize(width: 0, height: 0)
    selectedImage.layer.shadowOpacity = 1.0
    selectedImage.layer.shadowRadius = 6.0
    selectedImage.clipsToBounds = true
    }
}

3 个答案:

答案 0 :(得分:1)

如果你在imageview上给角半径和阴影,则不起作用。尝试在视图中使用imageview,然后在视图上给出角半径,在imageview上给出阴影

答案 1 :(得分:0)

尝试: selectedImage.layer.masksToBounds = true

检查aspectMode并检查剪辑子视图。

enter image description here

答案 2 :(得分:0)

您需要将UIView作为影子容器,然后将UIImageView添加到该容器中。以下是使阴影和角半径与UIImageView一起使用所需的示例。

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    let shadowView = UIView(frame: CGRect(x: 100, y: 100, width: 128, height: 128))
    shadowView.clipsToBounds = false
    shadowView.layer.shadowColor = UIColor(white: 0.0, alpha: 0.5).cgColor
    shadowView.layer.shadowOpacity = 1.0
    shadowView.layer.shadowOffset = CGSize.zero
    shadowView.layer.shadowRadius = 6.0
    shadowView.layer.shadowPath = UIBezierPath(roundedRect: shadowView.bounds, cornerRadius: 6.0).cgPath
    view.addSubview(shadowView)

    let imageView = UIImageView()
    imageView.image = UIImage(named: "YOUR_IMAGE_NAME")
    imageView.frame = shadowView.bounds
    imageView.clipsToBounds = true
    imageView.layer.cornerRadius = 5
    shadowView.addSubview(imageView)
}