一つのアイコンファイルからiOSアプリのアイコンサイズにリサイズしてセットするプログラムを作ったよ

qiitaにあった「アプリのアイコンを自動生成する」をxcode7.2でも使えるようにした

の副産物的な。

xcodeprojがあるディレクトリにicon.png(1024x1024)かicon.pdfを置いて実行するだけ!
超簡単!
PDF優先で使います。

resolutions.ymlも必要なんで上の記事からとってきてiconResize.rbと同ディレクトリにおいてください。

こちらもRMagickとImageMagick、あとGhostScriptとRGhostが必要です。

iTunesArtworkも生成するけど手動でプロジェクトにセットしてください。
#!/usr/bin/env ruby
require "pathname"
require "yaml"
require "json"
require "rubygems"
require "RMagick"
require "rghost"
RESOLUTIONS_LIST_PATH = File.expand_path("./resolutions.yml", __dir__)
def run
generate_identicons
generate_contents_json
edit_project_pbxproj
end
def get_appiconset_path
path = Pathname.glob(@current_path.to_s + "/**/*.xcassets").first
path = path.join("IconSet.appiconset")
path.mkdir unless path.exist?
path
end
def generate_identicons
resolutions = YAML.load_file(RESOLUTIONS_LIST_PATH)
resolutions.each do |device, images|
images.each do |image|
filepath = @appiconset_path.join(image["filename"])
size = image["size"].to_f
printf("\n size : %d",size)
size /= 2 if image["scale"] == "2x"
size /= 3 if image["scale"] == "3x"
grid_size = 8
square_size = image["size"].to_i / grid_size
border_size = ((image["size"].to_i - grid_size * square_size).to_f / 2).ceil
printf("\nborder_size:%d grid_size:%d square_size:%d size:%s\n",border_size,grid_size,square_size,image["size"])
original = Magick::Image.read(@iconsource).first
putsize = original.columns
size = image["size"].to_f
if putsize != size then
image = original.resize(size,size)
image.write(filepath)
end
end
end
end
def generate_contents_json
contents = { "images" => [] }
resolutions = YAML.load_file(RESOLUTIONS_LIST_PATH)
resolutions.each do |device, properties|
properties.each do |property|
size = property["size"].to_i
size = property["size"].to_f if size == 167
size /= 2 if property["scale"] == "2x"
size /= 3 if property["scale"] == "3x"
image = {}
image["idiom"] = device
image["filename"] = property["filename"]
image["size"] = "#{size}x#{size}"
image["scale"] = property["scale"]
contents["images"] << image
end
end
contents["info"] = { "version" => 1, "author" => "IconSet" }
filepath = @appiconset_path.join("Contents.json")
filepath.open("wb") do |file|
file << JSON.pretty_generate(contents)
end
end
def edit_project_pbxproj
data = ""
pbxproj_path = Pathname.glob(@current_path.to_s + "/**/project.pbxproj").first
pbxproj_path.open("rb") do |file|
data = file.read
end
data.gsub!(/(ASSETCATALOG_COMPILER_APPICON_NAME = ).*/) { "#{$1}IconSet;" }
pbxproj_path.open("wb") do |file|
file.flush
file << data
end
end
def iconcheck
if @iconsource.nil?
printf("\nアイコンファイルが見つかりません。\n")
exit(10)
end
end
def pfcheck
pf = Pathname.glob(@current_path.to_s + "/*.xcodeproj").first
if pf.nil?
printf("\nプロジェクトファイルが見つかりません。\n")
exit(10)
end
end
def pdfcheck
if @iconpdf.nil?
else
printf("\nPDFファイルが見つかりました。\nPDFファイルを元に生成します。\n")
RGhost::Config::GS[:path]='/opt/local/bin/gsc'
tmp = RGhost::Convert.new(@iconpdf.to_s)
size = (1024.0*2.0)/(76.0/72.0)
tmp.to :png, :resolution => size,:dTextAlphaBits => 4,:dGraphicsAlphaBits => 4, :filename => @current_path.to_s + "/icon.png"
end
end
def itunesartwork
original = Magick::Image.read(@iconsource).first
iTunesArtwork2x = original.resize(1024,1024)
iTunesArtwork2x.write(@current_path.to_s + "/iTunesArtwork@2x")
iTunesArtwork1x = original.resize(512,512)
iTunesArtwork1x.write(@current_path.to_s + "/iTunesArtwork")
end
@current_path = Pathname.pwd
@iconpdf = Pathname.glob(@current_path.to_s + "/icon.pdf").first
pdfcheck
@iconsource = Pathname.glob(@current_path.to_s + "/icon.png").first
iconcheck
pfcheck
itunesartwork
@appiconset_path = get_appiconset_path
run
view raw iconResize.rb hosted with ❤ by GitHub

コメント

このブログの人気の投稿

セサミサイクル1(初代)が壊れた

LinkBudsのバッテリー交換をしてみた

特価のiPhone14を契約してきた