Skip to content

Instantly share code, notes, and snippets.

@endemic
Created October 31, 2014 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save endemic/8a755392489df0dce5dd to your computer and use it in GitHub Desktop.
Save endemic/8a755392489df0dce5dd to your computer and use it in GitHub Desktop.
Script to create Xcode image assets
#!/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