Skip to content

Instantly share code, notes, and snippets.

@KoCMoHaBTa
Created May 4, 2016 12:13
Show Gist options
  • Save KoCMoHaBTa/09ffcb455279ed9aa30d980871394a42 to your computer and use it in GitHub Desktop.
Save KoCMoHaBTa/09ffcb455279ed9aa30d980871394a42 to your computer and use it in GitHub Desktop.
#!/usr/bin/swift
import Foundation
import Cocoa
extension String: ErrorType {}
extension NSImage {
public func writeToURL(url: NSURL, size: NSSize) throws {
let bitmap = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: Int(size.width), pixelsHigh: Int(size.height), bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: NSDeviceRGBColorSpace, bytesPerRow: 0, bitsPerPixel: 0)!
bitmap.size = NSSize(width: size.width, height: size.height)
NSGraphicsContext.saveGraphicsState()
NSGraphicsContext.setCurrentContext(NSGraphicsContext(bitmapImageRep: bitmap))
self.drawInRect(NSRect(origin: NSPoint(), size: size), fromRect: NSRect(), operation: .CompositeCopy, fraction: 1)
NSGraphicsContext.restoreGraphicsState()
try bitmap.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: [NSImageCompressionFactor: 1.0])?.writeToURL(url, options: [])
}
}
extension NSSize {
func scaleDown(scale: CGFloat) -> CGSize {
return CGSize(width: self.width / scale, height: self.height / scale)
}
func scaleUp(scale: CGFloat) -> CGSize {
return CGSize(width: self.width * scale, height: self.height * scale)
}
}
extension NSURL {
var fileName: String? {
return self.URLByDeletingPathExtension?.lastPathComponent ?? self.lastPathComponent
}
func URLByRenamingFileName(name: String, ext: String? = nil) -> NSURL? {
let baseURL = self.URLByDeletingLastPathComponent
var result = baseURL?.URLByAppendingPathComponent(name)
if let ext = ext ?? self.pathExtension {
result = result?.URLByAppendingPathExtension(ext)
}
return result
}
}
do {
guard Process.arguments.count > 1 else {
throw "You must provide a path to a directory as first argiment"
}
let path = Process.arguments[1]
let dir = NSURL(fileURLWithPath: path)
var files = try NSFileManager.defaultManager().contentsOfDirectoryAtURL(dir, includingPropertiesForKeys: nil, options: [.SkipsHiddenFiles, .SkipsPackageDescendants, .SkipsSubdirectoryDescendants])
files = files.filter({ $0.pathExtension == "png" })
try files.forEach({ (file) in
var file = file
let baseFileName = file.fileName!.stringByReplacingOccurrencesOfString("@3x", withString: "")
//rename to @3x if not named already
if file.lastPathComponent?.containsString("@3x") == false {
let newName = baseFileName + "@3x"
let renamedFile = file.URLByRenamingFileName(newName)!
try NSFileManager.defaultManager().moveItemAtURL(file, toURL: renamedFile)
file = renamedFile
}
let firstScreenScaleFactor = NSScreen.screens()?.first?.backingScaleFactor
let mainScreenScaleFactor = NSScreen.mainScreen()?.backingScaleFactor
let scaleFactor = firstScreenScaleFactor ?? mainScreenScaleFactor ?? 1
let _3xImage = NSImage(contentsOfFile: file.path!)!
let _3xImageRepresentation = NSBitmapImageRep(data: _3xImage.TIFFRepresentation!)!
let _3xSize = NSSize(width: _3xImageRepresentation.pixelsWide, height: _3xImageRepresentation.pixelsHigh)
//generate @1x
let _1xSize = _3xSize.scaleDown(3)
let _1xFile = file.URLByRenamingFileName(baseFileName)!
try _3xImage.writeToURL(_1xFile, size: _1xSize)
//generate @2x
let _2xSize = _1xSize.scaleUp(2)
let _2xFile = file.URLByRenamingFileName(baseFileName + "@2x")!
try _3xImage.writeToURL(_2xFile, size: _2xSize)
})
}
catch let e {
print(e)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment