Created
February 11, 2016 15:07
-
-
Save ahmattox/29b8c20bb47957758303 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Resizes an image file to necessary sizes for iOS application icons. | |
// Accepts the path to the file to be resized, e.g. `swift icons.swift icon.png` | |
// Created by Anthony Mattox on 2016-02-11 | |
import Cocoa | |
extension NSImage { | |
func resize(size: CGSize) -> NSImage { | |
let cgImage = self.CGImageForProposedRect(nil, context: nil, hints: nil) | |
let bitsPerComponent = CGImageGetBitsPerComponent(cgImage) | |
let bytesPerRow = CGImageGetBytesPerRow(cgImage) | |
let colorSpace = CGImageGetColorSpace(cgImage) | |
let bitmapInfo = CGImageGetBitmapInfo(cgImage) | |
let context = CGBitmapContextCreate(nil, Int(size.width), Int(size.height), bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo.rawValue) | |
CGContextSetInterpolationQuality(context, .High) | |
CGContextDrawImage(context, CGRect(origin: CGPointZero, size: size), cgImage) | |
let resizedCGImage = CGBitmapContextCreateImage(context)! | |
return NSImage(CGImage: resizedCGImage, size: size) | |
} | |
func savePNG(file: String) -> Bool { | |
guard let tiffData = TIFFRepresentation else { return false } | |
guard let bitmap = NSBitmapImageRep(data:tiffData) else { return false } | |
guard let pngData = bitmap.representationUsingType(.NSPNGFileType, properties: [:]) else { return false } | |
pngData.writeToFile(file, atomically: false) | |
return true | |
} | |
} | |
struct IconFormat { | |
let points: CGFloat | |
let scale: CGFloat | |
init(_ points: CGFloat, at scale: CGFloat) { | |
self.points = points | |
self.scale = scale | |
} | |
var pointSize: CGSize { | |
return CGSize(width: points, height: points) | |
} | |
var pixelSize: CGSize { | |
return CGSize(width: points * scale, height: points * scale) | |
} | |
var fileName: String { | |
if scale > 1 { | |
return String(format: "icon-%0.0f@%0.0fx.png", points, scale) | |
} | |
return String(format: "icon-%0.0f.png", points) | |
} | |
} | |
// The specific icon formats that should be created. These could be expanded to include more icon types, or there could be a way to provide them as a parameter to this script. | |
let iconFormats = [ | |
IconFormat(29, at: 1), | |
IconFormat(29, at: 2), | |
IconFormat(29, at: 3), | |
IconFormat(40, at: 1), | |
IconFormat(40, at: 2), | |
IconFormat(40, at: 3), | |
IconFormat(60, at: 2), | |
IconFormat(60, at: 3), | |
IconFormat(29, at: 1), | |
IconFormat(29, at: 2), | |
IconFormat(40, at: 1), | |
IconFormat(40, at: 2), | |
IconFormat(76, at: 1), | |
IconFormat(76, at: 2), | |
IconFormat(83.5, at: 2) | |
] | |
guard Process.arguments.count == 2 else { | |
print("Provide a filename to the original image.") | |
exit(0) | |
} | |
let inputImage = Process.arguments[1] | |
guard let image = NSImage(contentsOfFile: inputImage) else { | |
print("Unable to load image \"\(inputImage)\".") | |
exit(0) | |
} | |
for format in iconFormats { | |
let resizedImage = image.resize(format.pixelSize) | |
let fileName = format.fileName | |
if !resizedImage.savePNG(fileName) { | |
print("Unable to save \"\(fileName)\".") | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment