ios - 如何在swift中以菱形显示图像?

标签 ios swift uiimageview

Image

当我给出宽度和高度 120 并应用圆角半径时,我想以菱形显示图像。我得到了大致的钻石形状,但没有得到精确的钻石形状,所以任何人都建议我它对我有帮助。

self.imageView.layer.cornerRadius = self.imageView.frame.size.width / 2
self.imageView.clipsToBounds = true

最佳答案

如果你有一个 ImageView 并想将它裁剪成菱形(菱形),你应该:

  • 创建菱形的UIBezierPath
  • 将其用作CAShapeLayer路径
  • CAShapeLayer 设置为 UIImageViewlayermask

在 Swift 3 及更高版本中,它可能看起来像:

extension UIView {
    func addDiamondMask(cornerRadius: CGFloat = 0) {
        let path = UIBezierPath()
        path.move(to: CGPoint(x: bounds.midX, y: bounds.minY + cornerRadius))
        path.addLine(to: CGPoint(x: bounds.maxX - cornerRadius, y: bounds.midY))
        path.addLine(to: CGPoint(x: bounds.midX, y: bounds.maxY - cornerRadius))
        path.addLine(to: CGPoint(x: bounds.minX + cornerRadius, y: bounds.midY))
        path.close()

        let shapeLayer = CAShapeLayer()
        shapeLayer.path = path.cgPath
        shapeLayer.fillColor = UIColor.white.cgColor
        shapeLayer.strokeColor = UIColor.white.cgColor
        shapeLayer.lineWidth = cornerRadius * 2
        shapeLayer.lineJoin = kCALineJoinRound
        shapeLayer.lineCap = kCALineCapRound

        layer.mask = shapeLayer
    }
}

因此,只需在 ImageView 上调用 addDiamondMask(cornerRadius:)(其中 cornerRadius 是可选的)。

imageView.addDiamondMask()

产生:

enter image description here

有关 Swift 2 版本,请参阅 previous revision of this answer .


圆角的替代算法可能是:

extension UIView {
    func addDiamondMask(cornerRadius: CGFloat = 0) {
        let path = UIBezierPath()

        let points = [
            CGPoint(x: bounds.midX, y: bounds.minY),
            CGPoint(x: bounds.maxX, y: bounds.midY),
            CGPoint(x: bounds.midX, y: bounds.maxY),
            CGPoint(x: bounds.minX, y: bounds.midY)
        ]

        path.move(to: point(from: points[0], to: points[1], distance: cornerRadius, fromStart: true))
        for i in 0 ..< 4 {
            path.addLine(to: point(from: points[i], to: points[(i + 1) % 4], distance: cornerRadius, fromStart: false))
            path.addQuadCurve(to: point(from: points[(i + 1) % 4], to: points[(i + 2) % 4], distance: cornerRadius, fromStart: true), controlPoint: points[(i + 1) % 4])
        }
        path.close()

        let shapeLayer = CAShapeLayer()
        shapeLayer.path = path.cgPath
        shapeLayer.fillColor = UIColor.white.cgColor
        shapeLayer.strokeColor = UIColor.clear.cgColor

        layer.mask = shapeLayer
    }

    private func point(from point1: CGPoint, to point2: CGPoint, distance: CGFloat, fromStart: Bool) -> CGPoint {
        let start: CGPoint
        let end: CGPoint

        if fromStart {
            start = point1
            end = point2
        } else {
            start = point2
            end = point1
        }
        let angle = atan2(end.y - start.y, end.x - start.x)
        return CGPoint(x: start.x + distance * cos(angle), y: start.y + distance * sin(angle))
    }

}

这里我在角落做四边形贝塞尔曲线,但我认为如果钻石完全拉长,圆角的效果会比上面的效果稍微好一些。

无论如何,这会产生:

enter image description here

关于ios - 如何在swift中以菱形显示图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38090718/

相关文章:

ios - 列出设备 iOS swift 中的所有音频文件

swift - Swift 中所有类的根类?

string - 从字符串中删除第 n 个字符

ios - 方向改变后缩放 UIImageView

swift - mask UIView/UIImageView 以剪切透明文本

ios - kCATransitionPush 应用于从 UIImageview 滑入/滑出图像,没有淡入淡出/透明效果 iOS

ios - 图像的表格显示

ios - Apple 智能横幅在 ios safari 中显示空栏(在元标记中是正确的)

ios - CouchDB Kitura 执行 View

iphone - 使用 presentsWithGesture 时未调用 UISplitViewControllerDelegate 方法?