【问题标题】:How to blur the border of the mask for an NSImage?如何模糊 NSImage 的蒙版边框?
【发布时间】:2022-01-22 22:50:12
【问题描述】:

我正在尝试快速模糊 NSImage 的蒙版边框,但我不知道该怎么做。掩码应该是从CGMutablePath 创建的,因此可以通过编程进行更改。这是它目前的样子: 这是它应该看起来的样子:

创建锐边遮罩的代码如下:

class MyView: NSView {
    
    override func draw(_ dirtyRect: NSRect) {
        
        let bottomLayer = CALayer()
        bottomLayer.bounds = self.bounds
        bottomLayer.position = CGPoint(x: self.bounds.size.width/2, y: self.bounds.size.height/2)
        bottomLayer.contents = NSImage(named: "bottom.jpg")
        bottomLayer.contentsGravity = .resize
        self.layer?.addSublayer(bottomLayer)
        
        let path = CGMutablePath()
        path.move(to: CGPoint(x: self.bounds.size.width/2, y: self.bounds.size.height/2))
        path.addCurve(to: CGPoint(x: 0.5*self.bounds.size.width, y: self.bounds.size.height), control1: CGPoint(x: 0.4*self.bounds.size.width, y: 0), control2: CGPoint(x: 0, y: self.bounds.size.height))
        path.addCurve(to: CGPoint(x: self.bounds.size.width/2, y: self.bounds.size.height/2), control1: CGPoint(x: self.bounds.size.width, y: self.bounds.size.height), control2: CGPoint(x: 0.8*self.bounds.size.width, y: 0))
        path.closeSubpath()
        
        let maskLayer = CAShapeLayer()
        maskLayer.path = path
        maskLayer.fillRule = .evenOdd
        maskLayer.borderWidth = 20
        
        let topLayer = CALayer()
        topLayer.bounds = self.bounds
        topLayer.position = CGPoint(x: self.bounds.size.width/2, y: self.bounds.size.height/2)
        topLayer.contents = NSImage(named: "top.jpg")
        topLayer.contentsGravity = .resize
        topLayer.masksToBounds = true
        topLayer.mask = maskLayer
        
        self.layer?.addSublayer(topLayer)
    }
}

我希望有一个很好的解决方案:)

弗雷德里克

【问题讨论】:

    标签: swift image blur core-image nsbezierpath


    【解决方案1】:

    尝试添加CAGradientLayer 而不是CAShapeLayer

    样本来源:

    class MyView: NSView {
        
        override func draw(_ dirtyRect: NSRect) {
            
            let bottomLayer = CALayer()
            bottomLayer.bounds = self.bounds
            bottomLayer.position = CGPoint(x: self.bounds.size.width/2, y: self.bounds.size.height/2)
            bottomLayer.contents = NSImage(named: "bottom")
            bottomLayer.contentsGravity = .resize
            self.layer?.addSublayer(bottomLayer)
            
            let path = CGMutablePath()
            path.move(to: CGPoint(x: self.bounds.size.width/2, y: self.bounds.size.height/2))
            path.addCurve(to: CGPoint(x: 0.5*self.bounds.size.width, y: self.bounds.size.height), control1: CGPoint(x: 0.4*self.bounds.size.width, y: 0), control2: CGPoint(x: 0, y: self.bounds.size.height))
            path.addCurve(to: CGPoint(x: self.bounds.size.width/2, y: self.bounds.size.height/2), control1: CGPoint(x: self.bounds.size.width, y: self.bounds.size.height), control2: CGPoint(x: 0.8*self.bounds.size.width, y: 0))
            path.closeSubpath()
            
            let maskLayer = CAGradientLayer()
            maskLayer.shadowRadius = 4
            maskLayer.shadowPath = path
            maskLayer.shadowOpacity = 1
            maskLayer.shadowOffset = CGSize.zero
            maskLayer.shadowColor = NSColor.green.cgColor
            
            let topLayer = CALayer()
            topLayer.bounds = self.bounds
            topLayer.position = CGPoint(x: self.bounds.size.width/2, y: self.bounds.size.height/2)
            topLayer.contents = NSImage(named: "top")
            topLayer.contentsGravity = .resize
            topLayer.masksToBounds = true
            topLayer.mask = maskLayer
            
            self.layer?.addSublayer(topLayer)
        }
    }
    

    输出

    【讨论】:

      猜你喜欢
      • 2019-05-12
      • 2016-04-11
      • 2020-05-05
      • 2015-12-16
      • 2019-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多