UIImage 在主包中加载错误图像
UIImage load wrong image in main bundle
我有一个主项目包含超过 10 个包。我有两张名为 map_bubble_right_normal
的图片,一张在主包中,另一张在子包中 (SCommon.bundle)。我写的代码如下:
UIImage *image = [UIImage imageNamed:@"map_bubble_right_normal"];
代码写在 AppDelegate
的 didFinishLaunchingWithOptions
中。
我想加载我保存在主包中的图像。但是,从子包 (SCommon.bundle).
加载图像的代码的结果
我想保存在主包中的图像可能是错误图像(可能是复制问题)。因此,我显示 ipa 内容以查看保存在根目录中的 "map_bubble_right_normal" 文件。图片是对的!!!!!
我不知道为什么 imageNamed:
会从子包 (SCommon.bundle) 加载图像。我尝试删除应用程序并重新启动 iPhone,结果是一样的。而且,我尝试删除 XCode 的派生数据,并清理项目。图像仍然从子包中加载(SCommon.bundle)。
另外,我在iOS8和iOS9设备上测试了这个问题。
暂时只能通过修改bundleId(main bundle)的方法解决问题
我知道UIImage的缓存特性,但是我不明白为什么会出现这种奇怪的场景。
恳请各位大神帮忙。谢谢~
要从主包加载图像试试这个:
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"map_bubble_right_normal" ofType:@"png"]];
如果你想从 mainBundle 加载试试这个代码:
NSBundle* myBundle = [NSBundle mainBundle];
NSString* myImage = [myBundle pathForResource:@"Seagull" ofType:@"jpg"];
如果你想从子包加载:
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"YourBundleName" ofType:@"bundle"];
NSBundle *subBundle = [NSBundle bundleWithPath:bundlePath];
NSString* myImage = [subBundle pathForResource:@"Seagull" ofType:@"jpg"];
参考here
我也遇到过同样的情况。而不是你上面提到的两个包,我没能用正确的名字显示正确的图像。最后,我发现关键点是图像的后缀是 jpg
而不是 png
。我换了一张png
的图片,都转好了。也许这是您发生的可能情况。试试看,祝你好运。 :)
PS:这里a related link作为参考。
谢谢大家的回答。我很抱歉误导了我们所有人。
我找到了问题的原因。错误图像不是来自子包,它来自 Assets.car。问题的原因实际上应该归咎于物理目录中的Image.assert。 CocoaPods 会将所有匹配的“*.assets”复制到 Assets.car.
我在Apple Developer forum提出了同样的问题,找到了问题的关键提示。
与Pods copy resource script overrides default xcasset bahaviour.
问题相关的Cocoapods问题
Pod_resources.sh 如果您的 Pod Repo 添加了任何名称匹配 *.xcassets
.
的文件夹,Pod_Root 中的每个 *.xcassets
脚本都会复制
Pod_resources.sh的关键代码:
if [[ -n "${WRAPPER_EXTENSION}" ]] && [ "`xcrun --find actool`" ] && [ -n "$XCASSET_FILES" ]
...
# Find all other xcassets (this unfortunately includes those of path pods and other targets).
OTHER_XCASSETS=$(find "$PWD" -iname "*.xcassets" -type d)
while read line; do
if [[ $line != "`realpath $PODS_ROOT`*" ]]; then
XCASSET_FILES+=("$line")
fi
...
fi
如果您的 Pod 添加了任何 xcassets
文件,条件 XCASSET_FILES
就会满足。
我有一个主项目包含超过 10 个包。我有两张名为 map_bubble_right_normal
的图片,一张在主包中,另一张在子包中 (SCommon.bundle)。我写的代码如下:
UIImage *image = [UIImage imageNamed:@"map_bubble_right_normal"];
代码写在 AppDelegate
的 didFinishLaunchingWithOptions
中。
我想加载我保存在主包中的图像。但是,从子包 (SCommon.bundle).
加载图像的代码的结果我想保存在主包中的图像可能是错误图像(可能是复制问题)。因此,我显示 ipa 内容以查看保存在根目录中的 "map_bubble_right_normal" 文件。图片是对的!!!!!
我不知道为什么 imageNamed:
会从子包 (SCommon.bundle) 加载图像。我尝试删除应用程序并重新启动 iPhone,结果是一样的。而且,我尝试删除 XCode 的派生数据,并清理项目。图像仍然从子包中加载(SCommon.bundle)。
另外,我在iOS8和iOS9设备上测试了这个问题。
暂时只能通过修改bundleId(main bundle)的方法解决问题
我知道UIImage的缓存特性,但是我不明白为什么会出现这种奇怪的场景。
恳请各位大神帮忙。谢谢~
要从主包加载图像试试这个:
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"map_bubble_right_normal" ofType:@"png"]];
如果你想从 mainBundle 加载试试这个代码:
NSBundle* myBundle = [NSBundle mainBundle];
NSString* myImage = [myBundle pathForResource:@"Seagull" ofType:@"jpg"];
如果你想从子包加载:
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"YourBundleName" ofType:@"bundle"];
NSBundle *subBundle = [NSBundle bundleWithPath:bundlePath];
NSString* myImage = [subBundle pathForResource:@"Seagull" ofType:@"jpg"];
参考here
我也遇到过同样的情况。而不是你上面提到的两个包,我没能用正确的名字显示正确的图像。最后,我发现关键点是图像的后缀是 jpg
而不是 png
。我换了一张png
的图片,都转好了。也许这是您发生的可能情况。试试看,祝你好运。 :)
PS:这里a related link作为参考。
谢谢大家的回答。我很抱歉误导了我们所有人。
我找到了问题的原因。错误图像不是来自子包,它来自 Assets.car。问题的原因实际上应该归咎于物理目录中的Image.assert。 CocoaPods 会将所有匹配的“*.assets”复制到 Assets.car.
我在Apple Developer forum提出了同样的问题,找到了问题的关键提示。
与Pods copy resource script overrides default xcasset bahaviour.
问题相关的Cocoapods问题Pod_resources.sh 如果您的 Pod Repo 添加了任何名称匹配 *.xcassets
.
*.xcassets
脚本都会复制
Pod_resources.sh的关键代码:
if [[ -n "${WRAPPER_EXTENSION}" ]] && [ "`xcrun --find actool`" ] && [ -n "$XCASSET_FILES" ]
...
# Find all other xcassets (this unfortunately includes those of path pods and other targets).
OTHER_XCASSETS=$(find "$PWD" -iname "*.xcassets" -type d)
while read line; do
if [[ $line != "`realpath $PODS_ROOT`*" ]]; then
XCASSET_FILES+=("$line")
fi
...
fi
如果您的 Pod 添加了任何 xcassets
文件,条件 XCASSET_FILES
就会满足。