I have the test code below which draws a box with some text in it. I would like to add an image to the drawing. The function getCompanyLogo returns an NSImage and I would like to then add this image to the drawing. I have read apple's archive doc at the Drawing with COCOA but do not see how to complete this.
How is this done? Thanks
import Cocoa
@IBDesignable class invoiceSectionTop: NSView {
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
let context = NSGraphicsContext.current?.cgContext
let length = CGPoint(x: 200, y: 50)
let p1 = CGPoint(x: 0, y: 50)
let shape = "square"
context!.beginPath()
context!.move(to: p1)
if shape == "line" {
let pointEnd = CGPoint(x: p1.x + length.x, y: p1.y + length.y)
context!.addLine(to: pointEnd)
} else if shape == "square" {
let p2 = CGPoint(x: p1.x + length.x, y: p1.y)
let p3 = CGPoint(x: p1.x + length.x, y: p1.y + length.y)
let p4 = CGPoint(x: p1.x, y: p1.y + length.y)
context!.addLine(to: p2)
context!.addLine(to: p3)
context!.addLine(to: p4)
context!.addLine(to: p1)
}
context!.setStrokeColor(red: 1, green: 1, blue: 1, alpha: 1.0)
context!.setFillColor(red: 0, green: 1, blue: 0, alpha: 1)
context!.setLineWidth(2.0)
context!.strokePath()
let image = getCompanyLogo() // getCompanyLogo returns a NSImage object
// NSGraphicsContext.current?.NSIm.draw(cgimage!, in: dirtyRect)
let textColor = NSColor(calibratedRed: 1, green: 1, blue: 1, alpha: 1.0)
let textColorB = NSColor(calibratedRed: 1, green: 1, blue: 1, alpha: 0.0)
let rect = CGRect(x: 200, y: 200, width: 30, height: 130)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let attr = [NSAttributedString.Key.paragraphStyle: paragraphStyle, NSAttributedString.Key.foregroundColor: textColor, NSAttributedString.Key.backgroundColor: textColorB, NSAttributedString.Key.font:NSFont.init(name: "HelveticaNeue-Thin", size: 14)]
let q: NSString = "hello, world"
let center = CGPoint(x: 0, y: 50)
q.draw(at: center, withAttributes: attr as [NSAttributedString.Key : Any])
}
}
Found the following code on gitHub
extension NSImage {
/// Create a CIImage using the best representation available
///
/// - Returns: Converted image, or nil
func asCIImage() -> CIImage? {
if let cgImage = self.asCGImage() {
return CIImage(cgImage: cgImage)
}
return nil
}
/// Create a CGImage using the best representation of the image available in the NSImage for the image size
///
/// - Returns: Converted image, or nil
func asCGImage() -> CGImage? {
var rect = NSRect(origin: CGPoint(x: 0, y: 0), size: self.size)
return self.cgImage(forProposedRect: &rect, context: NSGraphicsContext.current, hints: nil)
}
}
extension CIImage {
/// Create a CGImage version of this image
///
/// - Returns: Converted image, or nil
func asCGImage(context: CIContext? = nil) -> CGImage? {
let ctx = context ?? CIContext(options: nil)
return ctx.createCGImage(self, from: self.extent)
}
/// Create an NSImage version of this image
/// - Parameters:
/// - pixelSize: The number of pixels in the result image. For a retina image (for example), pixelSize is double repSize
/// - repSize: The number of points in the result image
/// - Returns: Converted image, or nil
#if os(macOS)
@available(macOS 10, *)
func asNSImage(pixelsSize: CGSize? = nil, repSize: CGSize? = nil) -> NSImage? {
let rep = NSCIImageRep(ciImage: self)
if let ps = pixelsSize {
rep.pixelsWide = Int(ps.width)
rep.pixelsHigh = Int(ps.height)
}
if let rs = repSize {
rep.size = rs
}
let updateImage = NSImage(size: rep.size)
updateImage.addRepresentation(rep)
return updateImage
}
#endif
}
extension CGImage {
/// Create a CIImage version of this image
///
/// - Returns: Converted image, or nil
func asCIImage() -> CIImage {
return CIImage(cgImage: self)
}
/// Create an NSImage version of this image
///
/// - Returns: Converted image, or nil
func asNSImage() -> NSImage? {
return NSImage(cgImage: self, size: .zero)
}
}
Added the following two lines to get what I needed
let cgimage: CGImage? = image.asCGImage() // Here comes the picture
NSGraphicsContext.current?.cgContext.draw(cgimage!, in: dirtyRect)