NSImageView with corner radius and shadow

Hi, I'm trying to add a drop shadow for NSImageView with NSBeizerPath but the shadow does not appear

//add cgPath var to NSBezierPath extension NSBezierPath {

  public var cgPath: CGPath {     let path = CGMutablePath()     var points = [CGPoint](repeating: .zero, count: 3)

    for i in 0 ..< elementCount {       let type = element(at: i, associatedPoints: &points)       switch type {       case .moveTo:         path.move(to: points[0])       case .lineTo:         path.addLine(to: points[0])       case .curveTo:         path.addCurve(to: points[2], control1: points[0], control2: points[1])       case .closePath:         path.closeSubpath()       @unknown default:         continue       }     }

    return path   } }

//View var albumImage = NSImageView()

       albumImage.layer?.shadowRadius = 10.0       albumImage.layer?.shadowColor = .black       albumImage.layer?.shadowOffset = CGSize(width: 10, height: 10)       albumImage.layer?.shadowOpacity = 1.0       albumImage.layer?.shadowPath = NSBezierPath(roundedRect: albumImage.bounds, xRadius: 28.0, yRadius: 28.0).cgPath

Hi @aviorrok, try putting your code in a code block, and use "Paste and Match Style", then it will be readable.

//add cgPath var to NSBezierPath
extension NSBezierPath {
    public var cgPath: CGPath {
        let path = CGMutablePath()
        var points = [CGPoint](repeating: .zero, count: 3)
        for i in 0 ..< elementCount {
            let type = element(at: i, associatedPoints: &points)
            switch type {
            case .moveTo:         path.move(to: points[0])
            case .lineTo:         path.addLine(to: points[0])
            case .curveTo:        path.addCurve(to: points[2], control1: points[0], control2: points[1])
            case .closePath:      path.closeSubpath()
            @unknown default:     continue
            }
        }
        return path
    }
}
//View var albumImage = NSImageView()
albumImage.layer?.shadowRadius = 10.0
albumImage.layer?.shadowColor = .black
albumImage.layer?.shadowOffset = CGSize(width: 10, height: 10)
albumImage.layer?.shadowOpacity = 1.0
albumImage.layer?.shadowPath = NSBezierPath(roundedRect: albumImage.bounds, xRadius: 28.0, yRadius: 28.0).cgPath

What is supposed draw the shadow ? You create the path, but you do not draw it.

You have also failed to set:

albumImage.wantsLayer = true
NSImageView with corner radius and shadow
 
 
Q