if captureFilePath != nil{ let image: NSImage = getImageFromURL(url: captureFilePath!)! image.lockFocus() NSColor.white.setStroke() let height: Int = Int(image.size.height) let width: Int = Int(image.size.width) let lineWidth = CGFloat(height > width ? height / 200 + 1: width / 200 + 1) NSBezierPath.setDefaultLineWidth(lineWidth) NSBezierPath.strokeLine(from: NSPoint.init(x: Int(left.stringValue)!, y: 0), to: NSPoint.init(x: Int(left.stringValue)!, y: height)) NSBezierPath.strokeLine(from: NSPoint.init(x: width - Int(right.stringValue)!, y: 0), to: NSPoint.init(x: width - Int(right.stringValue)!, y: height)) NSBezierPath.strokeLine(from: NSPoint.init(x: 0, y: Int(bottom.stringValue)!), to: NSPoint.init(x: width, y: Int(bottom.stringValue)!)) NSBezierPath.strokeLine(from: NSPoint.init(x: 0, y: height - Int(top.stringValue)!), to: NSPoint.init(x: width, y: height - Int(top.stringValue)!)) image.unlockFocus() sourceImage.image = image }
效果如下图,其中白色的线就是用NSBezierPath画的