-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicony.swift
169 lines (133 loc) · 5.64 KB
/
icony.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env xcrun swift
import Foundation
import AppKit
// MARK: - ARGUMENTS CHECK
//TODO: remove this part
let argsCount = Process.arguments.count
for i in 1..<argsCount {
let s = Process.arguments[i]
println(" arg:\(s)")
}
// MARK: - TOOLS
extension NSImage {
// / Returns an image with the long edge being maxLongEdge
func resize(#maxLongEdge: CGFloat) -> NSImage? {
let longEdge = max(size.width, size.height)
let shortEdge = min(size.width, size.height)
if longEdge <= maxLongEdge {
return self
}
let scale = maxLongEdge/longEdge
if longEdge == size.width {
return resized(CGSize(width: maxLongEdge, height: shortEdge * scale))
} else {
return resized(CGSize(width: shortEdge * scale, height: maxLongEdge))
}
}
/// Returns an image in the given size (in pixels)
func resized(size: CGSize) -> NSImage? {
let image = self.CGImageForProposedRect(nil, context: nil, hints: nil)?.takeRetainedValue()
let bitsPerComponent = CGImageGetBitsPerComponent(image)
let bytesPerRow = CGImageGetBytesPerRow(image)
let colorSpace = CGImageGetColorSpace(image)
let bitmapInfo = CGImageGetBitmapInfo(image)
let context = CGBitmapContextCreate(nil, Int(size.width), Int(size.height), bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo)
CGContextSetInterpolationQuality(context, kCGInterpolationMedium)
CGContextDrawImage(context, CGRect(origin: CGPointZero, size: size), image)
return NSImage(CGImage: CGBitmapContextCreateImage(context), size: size)
}
/// Saves the image to file
func save(path: String) -> Bool {
if let cgRef = self.CGImageForProposedRect(nil, context: nil, hints: nil)?.takeRetainedValue() {
let newRep = NSBitmapImageRep(CGImage: cgRef)
newRep.size = self.size
let pngData = newRep.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: [:])
if let writeResult = pngData?.writeToFile(path, atomically: true) {
return writeResult
}
}
return false
}
}
extension CGSize {
var present: String {
return "\(Int(self.width))X\(Int(self.height))"
}
}
//===-------------------------------------------------------------------===##\\
// MARK: - Set path to save files
let defStPath = NSHomeDirectory() + "/Desktop"
var startPath = defStPath
let fileMan = NSFileManager.defaultManager()
if argsCount > 2 {
var givenPath = Process.arguments[2]
// givenPath = givenPath.stringByReplacingOccurrencesOfString("~", withString: NSHomeDirectory())
if fileMan.fileExistsAtPath(givenPath) {
startPath = givenPath
} else {
let prefixValid = givenPath.hasPrefix("~") || givenPath.hasPrefix("/")
if prefixValid && fileMan.createDirectoryAtPath(givenPath, withIntermediateDirectories: true, attributes: nil, error: nil) {
println("X given path does not exists, creating necessarily folders for path: \(givenPath)")
startPath = givenPath
} else {
println("X given path does not exists or invalid: \(givenPath)\n -> Using '~/Desktop/icons' instead")
}
}
}
var resultFolder = "icons"
var mainPath = startPath.stringByAppendingPathComponent(resultFolder)
// Create missing paths
let path = mainPath.stringByAppendingPathComponent("AppIcon.appiconset/")
fileMan.createDirectoryAtPath(path, withIntermediateDirectories: true, attributes: nil, error: nil)
// MARK: - EXECUTION
var fail = false
if argsCount > 1 {
if let img = NSImage(contentsOfFile: Process.arguments[1]) {
// Check image size
if img.size != CGSize(width: 1024, height: 1024) {
let sizeDesc = img.size.present
println("X Warning: Image size (\(sizeDesc)) is not the recommended size (1024X1024)")
}
// Resize and save files
let sizesAndPaths = [
("[email protected]", 1024),//also making sure its PNG
("iTunesArtwork.png", 512),
("AppIcon.appiconset/Icon-40.png", 40),
("AppIcon.appiconset/[email protected]", 80),
("AppIcon.appiconset/[email protected]", 120),
("AppIcon.appiconset/[email protected]", 120),
("AppIcon.appiconset/[email protected]", 180),
("AppIcon.appiconset/Icon-76.png", 76),
("AppIcon.appiconset/[email protected]", 152),
("AppIcon.appiconset/Icon-Small.png", 29),
("AppIcon.appiconset/[email protected]", 58),
("AppIcon.appiconset/[email protected]", 87)
]
for (relativePath, diameter) in sizesAndPaths {
let path = mainPath.stringByAppendingPathComponent(relativePath)
let size = CGSizeMake(CGFloat(diameter), CGFloat(diameter))
if let sized = img.resized(size) {
let saved = sized.save(path)
if saved == false {
println("X error: can't save image for size \(size.present)")
fail = true
}
} else {
println("X error: can't resize to \(size.present)")
fail = true
}
}
} else {
println("X can't get image from path - check your path")
fail = true
}
} else {
println("X no image path given")
fail = true
}
if !fail {
println("\n\n 🍕 All Done! 🍻\n\n")
exit(0)
} else {
exit(1)
}