iOS11 图片加载不出来
2017-10-31 本文已影响1131人
BinaryBang
1 现象描述
今天写demo的时候,拖了一个图片文件夹到项目:

然后各种找不到图片:
UIImage *image1 = [UIImage imageNamed:@"test1.jpg"];
NSString *path = [[NSBundle mainBundle] pathForResource:@"test1.jpg" ofType:nil];
UIImage *image2 = [UIImage imageWithContentsOfFile:path];
UIImage *image3 = [UIImage imageNamed:@"Ship.png"];

2 解决方法
最后发现,是因为在项目配置中,没有包含我拖进去的这些图片资源.即Target->BuildPhases->CopyBundleResources中没有包含这些图片资源.

自己手动点击加号包含图片:

然后这样就好了.
3 总结
这个现象是Xcode9才发现的,以前都没有遇到过,是因为以前的Xcode在拖入资源文件后,会自动把这些资源包含进来的,这一点我自己特意拿同事之前的Xcode验证过了.
网上都没有发现类似的问题,难道是我的Xcode9有问题么?