Created
October 27, 2014 17:11
-
-
Save jmcd/3dcb2a5492a9737512d2 to your computer and use it in GitHub Desktop.
Create all the different sized iOS App Icons from a single source image
This file contains 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
#!/usr/bin/xcrun swift | |
import Cocoa | |
import AppKit | |
import CoreGraphics | |
class Spec { | |
let pointSize: Int; | |
let scales: [Int] | |
init(pointSize: Int, atScale scales: [Int]){ | |
self.pointSize = pointSize; | |
self.scales = scales; | |
} | |
convenience init(pointSize: Int){ | |
self.init(pointSize: pointSize, atScale: [1, 2, 3]) | |
} | |
func edgeLen(atScale scale: Int)-> Int { | |
return pointSize*scale | |
} | |
} | |
var specs = [ | |
Spec(pointSize: 29), | |
Spec(pointSize: 40), | |
Spec(pointSize: 60), | |
Spec(pointSize: 76, atScale: [1,2]), | |
] | |
var inputFilePath = Process.arguments[1] | |
var inputImage = NSImage(byReferencingFile: inputFilePath) | |
var inputImageBitmapRepOrNil:NSBitmapImageRep? | |
for rep in inputImage?.representations as [NSImageRep] { | |
if let candidate = rep as? NSBitmapImageRep { | |
inputImageBitmapRepOrNil = candidate | |
} | |
} | |
if let inputImageBitmapRep = inputImageBitmapRepOrNil? { | |
var baseOutputFilePath = inputFilePath.stringByDeletingPathExtension | |
for spec in specs { | |
for scale in spec.scales { | |
var dim = spec.edgeLen(atScale: scale) | |
var fdim = CGFloat(dim) | |
var size = CGSizeMake(fdim, fdim) | |
var rect = CGRectMake(0.0, 0.0, fdim, fdim) | |
var outputBitmapRep = NSBitmapImageRep( | |
bitmapDataPlanes: nil, | |
pixelsWide: dim, | |
pixelsHigh: dim, | |
bitsPerSample: inputImageBitmapRep.bitsPerSample, | |
samplesPerPixel: inputImageBitmapRep.samplesPerPixel, | |
hasAlpha: inputImageBitmapRep.alpha, | |
isPlanar: inputImageBitmapRep.planar, | |
colorSpaceName: inputImageBitmapRep.colorSpaceName, | |
bitmapFormat: inputImageBitmapRep.bitmapFormat, | |
bytesPerRow: 0, | |
bitsPerPixel: inputImageBitmapRep.bitsPerPixel | |
) | |
var graphicsContext = NSGraphicsContext(bitmapImageRep: outputBitmapRep!) | |
if let c = graphicsContext?.CGContext { | |
var cgImage = inputImage?.CGImageForProposedRect(nil, context: graphicsContext, hints: nil)?.takeUnretainedValue() | |
CGContextDrawImage(c, rect, cgImage) | |
var data = outputBitmapRep!.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties:[:]) | |
var outputFilePath = "\(baseOutputFilePath)\(spec.pointSize)pt@\(scale)x.png" | |
data!.writeToFile(outputFilePath, atomically:false) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run
To get