转载,参考出处! github.com/nixzhu/dev-…
引用一下:
对于 app 内的图片,我们可用其名字获取:
let image = UIImage(named: "test_image") 复制代码
但这样并不安全,我们可能拼错图片的名字,图片本身也可能被删除。而如果我们要在多处使用同一张图片,就更要时时小心。
我们可以用一个 UIImage 的扩展来消除我们的担忧:
extension UIImage { static var xxx_testImage: UIImage { return UIImage(named: "test_image")! } } 复制代码
之后我们使用时,只需:
let image = UIImage.xxx_testImage 复制代码
我第一次使用,不知道太多命令无法得到想要的结果只能参照原文思想行动
1:搜索打印所有包含 “png”字段文件路径 ( /Volumes/US/XCode/animation-swift/animation-swift/Assets.xcassets 替换成自己的image路径);基本Contents.json都会包含.png文件,所以会打印所有Contents.json路径,注意也有重复路径(这个后面解决)
grep -l -r png /Volumes/US/XCode/animation-swift/animation-swift/Assets.xcassets
复制代码
输出:
/Volumes/US/XCode/animation-swift/animation-swift/Assets.xcassets/测试报告部分切图/亮泽度.imageset/Contents.json
/Volumes/US/XCode/animation-swift/animation-swift/Assets.xcassets/测试报告部分切图/人物头像.imageset/Contents.json
/Volumes/US/XCode/animation-swift/animation-swift/Assets.xcassets/测试报告部分切图/分数底色常态.imageset/Contents.json
/Volumes/US/XCode/animation-swift/animation-swift/Assets.xcassets/测试报告部分切图/分数底色点击.imageset/Contents.json
/Volumes/US/XCode/animation-swift/animation-swift/Assets.xcassets/测试报告部分切图/图层42.imageset/Contents.json
/Volumes/US/XCode/animation-swift/animation-swift/Assets.xcassets/测试报告部分切图/头像编号底色.imageset/Contents.json
/Volumes/US/XCode/animation-swift/animation-swift/Assets.xcassets/测试报告部分切图/头像编号底色.imageset/头像编号底色@2x.png
/Volumes/US/XCode/animation-swift/animation-swift/Assets.xcassets/测试报告部分切图/头像编号底色.imageset/头像编号底色@3x.png
复制代码
2:awk -F '.imageset' '{print $1}' 以 '.imageset'分割字符串,'{print $1}'输出前一部分
grep -l -r png /Volumes/US/XCode/animation-swift/animation-swift/Assets.xcassets | awk -F '.imageset' '{print $1}'
复制代码
输出:
/Volumes/US/XCode/animation-swift/animation-swift/Assets.xcassets/测试报告部分切图/亮泽度
/Volumes/US/XCode/animation-swift/animation-swift/Assets.xcassets/测试报告部分切图/人物头像
/Volumes/US/XCode/animation-swift/animation-swift/Assets.xcassets/测试报告部分切图/分数底色常态
/Volumes/US/XCode/animation-swift/animation-swift/Assets.xcassets/测试报告部分切图/分数底色点击
/Volumes/US/XCode/animation-swift/animation-swift/Assets.xcassets/测试报告部分切图/图层42
/Volumes/US/XCode/animation-swift/animation-swift/Assets.xcassets/测试报告部分切图/头像编号底色
/Volumes/US/XCode/animation-swift/animation-swift/Assets.xcassets/测试报告部分切图/头像编号底色
/Volumes/US/XCode/animation-swift/animation-swift/Assets.xcassets/测试报告部分切图/头像编号底色
复制代码
3:awk -F '/' '{print NF}'输出最后部分;uniq 前面提到的去除重复项,awk '{ gsub(/-/, ""); print }' 用“”替换"-"
grep -l -r png /Volumes/US/XCode/animation-swift/animation-swift/Assets.xcassets | awk -F '.imageset' '{print $1}' | awk -F '/' '{print $NF}' | awk '{ gsub(/-/, "_"); print }' | uniq
复制代码
输出:
亮泽度
人物头像
分数底色常态
分数底色点击
图层42
头像编号底色
复制代码
4:awk '{print "static var imageName_"$1": UIImage {\n\treturn UIImage(named: ""$1"")!\n}\n"}'关于这一段看大神写的介绍 github.com/nixzhu/dev-…,用于生产如下代码:
static var xxx_testImage: UIImage { return UIImage(named: "test_image")! }
grep -l -r png /Volumes/US/XCode/animation-swift/animation-swift/Assets.xcassets | awk -F '.imageset' '{print $1}' | awk -F '/' '{print $NF}' | awk '{ gsub(/-/, "_"); print }' | uniq | awk '{print "static var imageName_"$1": UIImage {\n\treturn UIImage(named: \""$1"\")!\n}\n"}'
复制代码
输出:
static var imageName_亮泽度: UIImage {
return UIImage(named: "亮泽度")!
}
static var imageName_人物头像: UIImage {
return UIImage(named: "人物头像")!
}
static var imageName_分数底色常态: UIImage {
return UIImage(named: "分数底色常态")!
}
static var imageName_分数底色点击: UIImage {
return UIImage(named: "分数底色点击")!
}
复制代码
Ok ,粘贴复制进代码就可以愉快的玩耍啦啦啦啦!