Ios Swift 4-在Swift中具有两个圆角的imageview?

Ios Swift 4-在Swift中具有两个圆角的imageview?,ios,swift,uicollectionview,uiimageview,swift4,Ios,Swift,Uicollectionview,Uiimageview,Swift4,我有一个UICollectionView单元格内的UIImageView,我想让它的顶角变圆。我无法解决我的问题,非常感谢您的帮助 你们可以看到,该单元的角半径为10px,我希望同样的效果也应用到图像上 您可以尝试UIRectCorner 这是我的定制课程 代码: 1。调整视图大小时调用。 uiimage.roundCorners([.topLeft, .topRight], radius: 10) 2。创建自定义类 class CustomImageView: UIImageView {

我有一个UICollectionView单元格内的UIImageView,我想让它的顶角变圆。我无法解决我的问题,非常感谢您的帮助

你们可以看到,该单元的角半径为10px,我希望同样的效果也应用到图像上


您可以尝试
UIRectCorner

这是我的定制课程

代码:

1。调整视图大小时调用。

uiimage.roundCorners([.topLeft, .topRight], radius: 10)
2。创建自定义类

class CustomImageView: UIImageView {
    override func layoutSubviews() {
        super.layoutSubviews()
        self.roundCorners([.topLeft, .topRight], radius: 10)
    }
}

在您的UICollectionViewCell中尝试此功能

 override func awakeFromNib() {
        super.awakeFromNib()
        DispatchQueue.main.async {
            self.image.roundCorners([.topRight,.topLeft], radius: 8)
            self.image.layer.masksToBounds = true
        }
    }
感谢和

以下代码适用于我的采集单元的ImageView左上角和右上角

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:TUGBucketCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! TUGBucketCell
        //Rounded corner ImageView
        DispatchQueue.main.async {
            cell.imgVw.roundCorners([.topLeft, .topRight], radius: 10)
            cell.imgVw.layer.masksToBounds = true
        }

        return cell
    }
分机是

extension UIImageView {
    public func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
        let maskPath = UIBezierPath(roundedRect: bounds,
                                    byRoundingCorners: corners,
                                    cornerRadii: CGSize(width: radius, height: radius))
        let shape = CAShapeLayer()
        shape.path = maskPath.cgPath
        layer.mask = shape
    }
}

Swift 5

在collectionview单元格中,输入下面两行代码&很好。无需编写任何扩展名:

cell._imageView.layer.cornerRadius = 10
cell._imageView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
此处将应用拐角半径左上角和右上角拐角和其余拐角将保持不变

如果要在左下角和右下角应用角半径,则遮罩角应如下所示:

cell._imageView.layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner]

希望有帮助。

你在图像视图的上角写的代码是什么?只要单元格的
clipsToBounds=true
和图像视图是该单元格内容视图的子视图,你就可以开始了(假设你已经使用
cornerRadius
获得圆角)@Alladinian clipsToBound=true,self.layer.cornerRadius=10,imageview是一个子视图,但仍然不能正常工作。听起来不错(我的意思是它应该可以正常工作)。。。您是否尝试过“调试视图层次结构”工具来检查单元格?此外,如果你隐藏图像,你会在单元格顶部得到圆角吗?@Alladinian下面的答案是正确的。我打错地方了。。新手错误。视图调整大小时调用。
cell._imageView.layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner]