一、前言
如果你刚刚升级了Xcode8,而你的项目的Deployment Target是iOS 9.3以下,运行iOS8的时候过了几十秒后crash到main函数,出现EXC_BAD_ACCESS,或者崩溃到imageNamed:,或者每次编译运行随机崩溃到某个地方。那么恭喜你,你读完这个文章你可能就解决了。
二、崩溃原因
在Xcode8中,如果你的图片资源文件里有16位图或者图片显示模式为P3,并且Deployment Target是iOS9.3以下的就会出现这个问题。(话说我公司的项目里面就出现了一个小按钮,导致了这次崩溃,不知道设计师是怎么弄出来的这个特殊图片…)如果你的App需要支持wide color functionality,那你就必须设置Deployment Target为iOS9.3以上。如果你的APP不需要支持wide color functionality并且你希望兼容iOS老版本,那么你需要将所有16-bit or P3 assets的图片转换为8-bit sRGB assets
三、定位问题图片
1.打一个ipa包,解压你的应用的ipa包,进入到你应用的Playload文件夹。
2.用find命令定位到Assets.car文件
find . -name 'Assets.car'
3.使用 assetutil 命令导出图片的信息存储到Assets.json文件中
sudo xcrun --sdk iphoneos assetutil --info /path/to/a/Assets.car > /tmp/Assets.json
4.打开刚才生成的Assets.json文件,查找含有”DisplayGamut” : “P3”, “Encoding” : “ARGB-16″的内容。这个对应的Name就是出现问题的图片了。
{
"SizeClass Vertical" : "universal",
"Graphics" : "GLES2,0",
"Name" : "ianisme.com",
"Scale" : 2,
"Idiom" : "universal",
"Memory" : "512MB",
"LayoutDirection" : "0 - Horizontal",
"DisplayGamut" : "P3",
"Encoding" : "ARGB-16",
"SizeClass Horizontal" : "universal",
"Image Type" : "kCoreThemeOnePartScale",
"AssetType" : "Image",
"Subtype" : 0,
"EdgeInsets" : "top:0 left:0 bottom:0 right:0"
},
四、转换图片为8-bit sRGB assets格式
这里我们使用bash script直接处理所有图片为正确格式,这样我们就不用去定位是哪个图片的问题了,或许更方便一些。
#!/bin/bash
DIRECTORY=$1
echo "------------------------------"
echo "Passed Resources with xcassets folder argument is <$DIRECTORY>"
echo "------------------------------"
echo "Processing asset:"
XSAASSETSD="$(find "$DIRECTORY" -name '*.xcassets')"
for xcasset in $XSAASSETSD
do
echo "---$xcasset"
IMAGESETS="$(find "$xcasset" -name '*.imageset')"
for imageset in $IMAGESETS
do
echo "------$imageset"
FILES="$(find "$imageset" -name '*.png')"
for file in $FILES
do
echo "---------$file"
sips -m "/System/Library/Colorsync/Profiles/sRGB Profile.icc" $file --out $file
done
done
done
echo "------------------------------"
echo "script successfully finished"
echo "------------------------------"
参考文献:
StackOverFlow
连接
中文技术支持