使用 XCode 6 / Swift 在 iOS 7.1 中未显示的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24063778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Images not showing in iOS 7.1 with XCode 6 / Swift
提问by pudge
I have this ObjC code:
我有这个 ObjC 代码:
[self.myButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@-button", self.myObject.name]]
forState:UIControlStateNormal];
This works great with these combinations:
这适用于这些组合:
- XCode 5, iOS 7.1
- XCode 6, iOS 7.1
- XCode 6, iOS 8
- XCode 5,iOS 7.1
- Xcode 6,iOS 7.1
- Xcode 6,iOS 8
But when I port one single class of the project to Swift -- a class which is unrelated to anything that is happening in this code -- the images do not display in XCode 6 for iOS 7.1. It does work with XCode 6 and iOS 8.
但是当我将项目的一个类移植到 Swift 时——一个与此代码中发生的任何事情都无关的类——图像不会在 iOS 7.1 的 XCode 6 中显示。它适用于 XCode 6 和 iOS 8。
I finished porting the entire project to Swift, so now the code looks like:
我完成了将整个项目移植到 Swift,所以现在代码如下所示:
self.myButton.setBackgroundImage(
UIImage(named:self.myObject.name + "-button"),
forState: UIControlState.Normal
)
And it's still unhappy on iOS 7.1. Still no images. (The custom icon works, though.) Yes, I know this is just beta software, and it's probably just a bug ... ? But I'm just wondering if anyone has a solution or insight.
而且在 iOS 7.1 上还是不爽。仍然没有图像。(虽然自定义图标有效。)是的,我知道这只是测试版软件,它可能只是一个错误......?但我只是想知道是否有人有解决方案或见解。
I only started using XCode and ObjC about a week ago (surprise! ObjC is now deprecated!), so it could be that I am missing something, but since it works in ObjC, and in Swift+iOS8, it seems like it's probably a bug.
我大约一周前才开始使用 XCode 和 ObjC(惊讶!ObjC 现在已被弃用!),所以可能是我遗漏了一些东西,但由于它在 ObjC 和 Swift+iOS8 中工作,似乎它可能是一个漏洞。
回答by mgcm
I've managed to overcome this by not usingAsset Libraries to store my images.
通过不使用资产库来存储我的图像,我设法克服了这个问题。
Add your images to the project using the "old style naming", like Image.pngand [email protected], then load them without any extensions in the filename, as such:
使用“旧样式命名”将您的图像添加到项目中,例如Image.png和[email protected],然后加载它们,文件名中没有任何扩展名,如下所示:
let myImage = UIImage(named:"Image")
回答by John Kakon
Fixed in XCode 6 beta 2
Images from asset catalogs in projects with a minimum deployment target of iOS 7 or OS X 10.9 will be available when running on iOS 8 and OS X 10.10, and now also iOS 7 and OS X 10.9. (17029658)
当在 iOS 8 和 OS X 10.10 上运行时,项目中资产目录中的图像将可用,最低部署目标为 iOS 7 或 OS X 10.9,现在也可用于 iOS 7 和 OS X 10.9。(17029658)
回答by Anton Gaenko
For me it was because I suddenly add jpgimage to Image Assets. Just resaved it as pngand all work well
对我来说这是因为我突然将jpg图像添加到Image Assets。只是将其重新保存为png并且一切正常
回答by A-Live
I guess you are testing it on simulator. Make sure the desired image is copied to 7.1 bundle. To do it check copy resources bundle
build stage or check manually app bundles for different simulators at ~/Library/Application Support/iPhone Simulator/
.
我猜你是在模拟器上测试它。确保将所需图像复制到 7.1 包。为此,请检查copy resources bundle
构建阶段或手动检查不同模拟器的应用程序包~/Library/Application Support/iPhone Simulator/
。
回答by johnboiles
@o KB opoints out herethat the Xcode6 Beta release notesmention that xcassets
bundles aren't supported for iOS7. Additionally, I've found it surprisingly hard to get rid of asset bundles in your project. This can cause naming collisions if your images have the same name as images that were previously in the bundle (you probably want the same names, so you don't have to rename your images everywhere in code / IB).
@o KB o在这里指出,Xcode6 Beta 发行说明提到xcassets
iOS7 不支持捆绑包。此外,我发现在您的项目中摆脱资产包非常困难。如果您的图像与之前在包中的图像具有相同的名称(您可能需要相同的名称,因此您不必在代码/IB 中的任何地方重命名图像),这可能会导致命名冲突。
Here's a workaround:
这是一个解决方法:
- Copy each image out of your
.xcassets
image bundle to a new directory (let's call itImages/
). See below for a script to make this easier. - Delete your
.xcassets
bundle. (surprisingly, removing it from the project isn't enough. In my testing, if the.xcassets
bundle was anywhere in the same directory as the Xcode project or related sources, it would get copied in. Alternately, you can remove the.xcassets
extension) - Add all your image files to your Xcode project
- Clean (cmd + shift + k)
- Delete the app from the target device or simulator
- Install and run
- 将
.xcassets
图像包中的每个图像复制到一个新目录(我们称之为Images/
)。请参阅下面的脚本以简化此操作。 - 删除您的
.xcassets
捆绑包。(令人惊讶的是,从项目中删除它是不够的。在我的测试中,如果.xcassets
捆绑包与 Xcode 项目或相关源位于同一目录中的任何位置,它将被复制进去。或者,您可以删除.xcassets
扩展名) - 将所有图像文件添加到 Xcode 项目中
- 清洁(cmd + shift + k)
- 从目标设备或模拟器中删除应用程序
- 安装并运行
To make step #1 less tedious, I wrote a script to copy images out of the .xcassets
bundle and into a directory of your choice: https://github.com/johnboiles/xcasset_exporter
为了使步骤 #1 不那么乏味,我编写了一个脚本来将图像从.xcassets
包中复制到您选择的目录中:https: //github.com/johnboiles/xcasset_exporter
mkdir Images
./xcasset_exporter.py MyProject/Images.xcassets Images
回答by Tiago A.
I had a problem where someimages in xcassets worked and others didn't (only for iOS 7.1). I solved it by deleting the problematic image sets and creating new ones with different names.
我遇到了一个问题,其中xcassets中的某些图像有效而其他图像无效(仅适用于 iOS 7.1)。我通过删除有问题的图像集并创建具有不同名称的新图像集来解决它。
回答by Sixto Saez
If you are attempting to use an external bundlewith proper PNG images stored in either an "old style" folder or inside xcassets then apps running on iOS 7 will notbe able to access these images.
如果您尝试使用带有存储在“旧样式”文件夹或 xcassets 中的适当 PNG 图像的外部包,则在 iOS 7 上运行的应用程序将无法访问这些图像。
I tried a suggestion from another answer here to add the "old style" folder path of the images in the external bundle but that didn't work for me. My solution, which fit my scenario, was to expose an outlet for the resource in the nib inside the external bundle. This allowed the main app to set with an accessible image to it. Whether this is a bug or simply a constraint in using nibs contained in external bundles with iOS 7 will matter less as it rides on to the usage sunset...
我在这里尝试了另一个答案的建议,在外部包中添加图像的“旧样式”文件夹路径,但这对我不起作用。我的解决方案适合我的场景,是在外部包内的 nib 中为资源公开一个出口。这允许主应用程序为其设置可访问的图像。这是一个错误还是仅仅是使用 iOS 7 的外部包中包含的笔尖的限制,随着使用日落的到来,它的重要性将降低......
PS:Apps running on iOS 8 where able to access images in the external bundle xcassets just fine so choose your poison for supporting iOS 7.
PS:在 iOS 8 上运行的应用程序能够访问外部包 xcassets 中的图像就好了,所以选择你的毒药来支持 iOS 7。