qiitaにあった「アプリのアイコンを自動生成する」をxcode7.2でも使えるようにした
xcodeで開発してるとiOSのホーム画面がすごくダサくなります
そんでなんかいいのないかなって思ったらこんなのが↓
アプリのアイコンを自動生成する
これはいい!と思い早速インストール!
実行!
はい!エラー!!!
見てみたらこの記事2013年のじゃないの
ってことでどうにかして直してみました。
修正箇所
1.bundle_identifierがxcode7からplistではなくxcodeproj内に記録されるようになったので
xcode7で作成されたプロジェクト(またはアップデートしたプロジェクト)ではxcodeprojから取得するように修正。
2.サイズがアイコンサイズになっていないのを修正(生成後サイズチェックし不一致であればRMagickでリサイズ)
3.iPadPro用のアイコンも作るようにした。
4.その他細かいところいろいろいじったけど忘れました。
使い方
1.qiita記事からpiconをインストール
2.上のresolutions.yml、generator.rbを上書き
3.RMagick※、pbxplorerインストール
4.あとはオリジナル版piconと一緒。
RMagickはImageMagickが必要です。
(というかImageMagickをRubyで使えるように受け渡ししてくれるやつっぽい。よくわかってないです)
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
require "pathname" | |
require "yaml" | |
require "json" | |
require "plist" | |
require "ruby_identicon" | |
require "rubygems" | |
require "RMagick" | |
require "pbxplorer" | |
module Picon | |
class Generator | |
RESOLUTIONS_LIST_PATH = File.expand_path("./resolutions.yml", __dir__) | |
def self.run | |
new.run | |
end | |
def initialize(args = {}) | |
if args[:current_path] | |
@current_path = Pathname.new(File.expand_path(args[:current_path])) | |
else | |
@current_path = Pathname.pwd | |
end | |
@product_name = get_product_name | |
@appiconset_path = get_appiconset_path | |
@bundle_identifier = get_bundle_identifier | |
end | |
def run | |
generate_identicons | |
generate_contents_json | |
edit_project_pbxproj | |
end | |
private | |
def get_product_name | |
xcodeproj_path = Pathname.glob(@current_path.to_s + "/**/*.xcodeproj").first | |
pbxproj_path = Pathname.glob(@current_path.to_s + "/**/*.xcodeproj/*.pbxproj").first | |
pf = XCProjectFile.new pbxproj_path | |
pname = pf.project.targets[0]["productName"] | |
pfs = pf.objects.to_s.match(/\"PRODUCT_BUNDLE_IDENTIFIER[^,]*/).to_s | |
#printf("\n%s",pf.objects.to_s.match(/\"PRODUCT_BUNDLE_IDENTIFIER[^,]*/).to_s.length) | |
@pbi = "" | |
@pbi = pfs.split("=>")[1].gsub!(/\"/) { "" } if pfs != "" | |
#printf("\n%s",pbi) | |
File.basename(pname)#xcodeproj_path.to_s, ".xcodeproj") | |
end | |
def get_appiconset_path | |
path = Pathname.glob(@current_path.to_s + "/**/*.xcassets").first | |
path = path.join("Picon.appiconset") | |
path.mkdir unless path.exist? | |
path | |
end | |
def get_bundle_identifier | |
pathnames = Pathname.glob(@current_path.to_s + "/**/*Info.plist") | |
#pathnames.reject! { |pathname| pathname.to_s =~ /Test/ } | |
pathnames.reject! { |pathname| pathname.to_s =~ /\/Test\// } | |
pathname = pathnames.first | |
plist = Plist.parse_xml(pathname.to_s) | |
plist["CFBundleIdentifier"].gsub!(/\$\(PRODUCT_BUNDLE_IDENTIFIER\)/) { @pbi } | |
plist["CFBundleIdentifier"].gsub!(/\$\(PRODUCT_NAME.*\)/) { @product_name } | |
printf("\n%s",plist["CFBundleIdentifier"]) | |
@bundle_identifier = plist["CFBundleIdentifier"] | |
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"]) | |
#next if filepath.exist? | |
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"]) | |
RubyIdenticon.create_and_save(@bundle_identifier, filepath, border_size: border_size, grid_size: grid_size, square_size: square_size.to_i ,background_color: 0xffffffff) | |
#sizecheck | |
original = Magick::Image.read(filepath).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" => "picon" } | |
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 | |
xcworkspace_path = Pathname.glob(@current_path.to_s + "/**/#{@product_name}.xcworkspace").first | |
if xcworkspace_path && xcworkspace_path.exist? | |
data.gsub!(/(<key>ASSETCATALOG_COMPILER_APPICON_NAME<\/key>\n\t+<string>).+(<\/string>)/) { "#{$1}Picon#{$2}" } | |
else | |
data.gsub!(/(ASSETCATALOG_COMPILER_APPICON_NAME = )AppIcon/) { "#{$1}Picon" } | |
end | |
pbxproj_path.open("wb") do |file| | |
file.flush | |
file << data | |
end | |
end | |
end | |
end | |
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
iphone: | |
- size: 58 | |
filename: "Icon-29@2x.png" | |
scale: "2x" | |
- size: 87 | |
filename: "Icon-29@3x.png" | |
scale: "3x" | |
- size: 80 | |
filename: "Icon-40@2x.png" | |
scale: "2x" | |
- size: 120 | |
filename: "Icon-40@3x.png" | |
scale: "3x" | |
- size: 120 | |
filename: "Icon-60@2x.png" | |
scale: "2x" | |
- size: 180 | |
filename: "Icon-60@3x.png" | |
scale: "3x" | |
ipad: | |
- size: 29 | |
filename: "Icon-29.png" | |
scale: "1x" | |
- size: 58 | |
filename: "Icon-29@2x.png" | |
scale: "2x" | |
- size: 40 | |
filename: "Icon-40.png" | |
scale: "1x" | |
- size: 80 | |
filename: "Icon-40@2x.png" | |
scale: "2x" | |
- size: 76 | |
filename: "Icon-76.png" | |
scale: "1x" | |
- size: 152 | |
filename: "Icon-76@2x.png" | |
scale: "2x" | |
- size: 167 | |
filename: "Icon-Pro.png" | |
scale: "2x" | |
コメント
コメントを投稿