Skip to content

Instantly share code, notes, and snippets.

@dbonates
Created February 21, 2020 20:58
Show Gist options
  • Select an option

  • Save dbonates/6dd7e2de5eb11eadf458e24a346db3d1 to your computer and use it in GitHub Desktop.

Select an option

Save dbonates/6dd7e2de5eb11eadf458e24a346db3d1 to your computer and use it in GitHub Desktop.
import AppKit
let currentDirectoryURL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
let imageURL = URL(fileURLWithPath: "logo.png", relativeTo: currentDirectoryURL)
let args = ProcessInfo.processInfo.arguments
let appVersion = args[1]
func write(_ text: String, on image :NSImage) -> NSImage
{
let font = NSFont.boldSystemFont(ofSize: 18)
let imageRect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
let textRect = CGRect(x: 5, y: 5, width: image.size.width - 5, height: image.size.height - 5)
let textStyle = NSMutableParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
let textFontAttributes = [
NSAttributedString.Key.font: font,
NSAttributedString.Key.foregroundColor: NSColor.white,
NSAttributedString.Key.paragraphStyle: textStyle
]
let im:NSImage = NSImage(size: image.size)
let rep:NSBitmapImageRep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: Int(image.size.width), pixelsHigh: Int(image.size.height), bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: NSColorSpaceName.calibratedRGB, bytesPerRow: 0, bitsPerPixel: 0)!
im.addRepresentation(rep)
im.lockFocus()
image.draw(in: imageRect)
text.draw(in: textRect, withAttributes: textFontAttributes)
im.unlockFocus()
return im
}
if let img = NSImage(contentsOf: imageURL.absoluteURL) {
let img = write(appVersion, on: img)
if let tiff = img.tiffRepresentation {
do {
let destUrl = URL(fileURLWithPath: "logo-\(appVersion).png", relativeTo: currentDirectoryURL)
try tiff.write(to: destUrl)
} catch let error {
print("erro ao salvar imagem")
print(error)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment