Created
October 31, 2014 11:03
-
-
Save endemic/8a755392489df0dce5dd to your computer and use it in GitHub Desktop.
Script to create Xcode image assets
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/env ruby | |
require "json" | |
require 'fileutils' | |
# Requires https://github.com/JamieMason/ImageOptim-CLI | |
Dir.glob("source/*.png") do |path| | |
file = path.split("/").pop | |
filename = file.split(".").shift | |
extension = file.split(".").pop | |
imageset_directory = "#{filename}.imageset" | |
destination = "destination/#{imageset_directory}" | |
FileUtils.mkdir_p(destination) | |
contents_json = { | |
images: [], | |
info: { | |
version: 1, | |
author: "Xcode" | |
} | |
} | |
[ | |
{ resize: 200, idiom: "ipad", scale: 1 }, | |
{ resize: 400, idiom: "ipad", scale: 2 }, | |
{ resize: 200, idiom: "iphone", scale: 2 }, | |
{ resize: 300, idiom: "iphone", scale: 3 }, | |
].each do |format| | |
new_filename ="#{filename}_#{format[:idiom]}_#{format[:scale]}x.#{extension}" | |
# Resize each file | |
`convert source/#{file} -resize #{format[:resize]}% -filter box #{destination}/#{new_filename}` | |
contents_json[:images] << { idiom: format[:idiom], scale: "#{format[:scale]}x", filename: new_filename } | |
print "." | |
end | |
# Optimize | |
#`imageOptim --directory #{destination}` | |
File.write("#{destination}/Contents.json", contents_json.to_json) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment