位于 IB 中可见但在运行时中不可见的单独包中的资源
我有一个应用程序,其中大部分图像资源都存储在单独的资源包中(这样做是有充分理由的)。该资源包与主应用程序包一起添加到项目中,当我在 Interface Builder 中设计 NIB 时,所有这些图像在 IB 中都可见(即它们出现在我的 UIImageView 的下拉列表中,并且它们正确显示在我的 UI 中) )。但是,当我在模拟器中运行我的应用程序时,所有这些图像都丢失了。除了主应用程序包之外,有没有办法告诉 UIKit 在这个附加资源包中查找这些图像?
我可以在 viewDidLoad 中手动加载这些图像,没有问题,但我正在寻找一种使用 Interface Builder 的方法,以便更轻松地更改这些 NIB 的设计/外观。
更新:虽然该捆绑包中的图像文件可直接在 IB 的下拉列表中使用,但它们不会在运行时加载。但是,如果我在 IB 中为它们的名称添加前缀,如下所示:
然后它们将在运行时正确加载,但 IB 会将它们显示为蓝色大问号。
现在的问题是 - 如何两全其美,并在 IB(设计时)和运行时正确显示图像。
I have an application, where most of image resources are stored in the separate resource bundle (there is a valid reason to do that). This resource bundle is added to the project along with main application bundle and while I design my NIB in the Interface Builder all those images are visible inside the IB (i.e. they appear in the drop list for my UIImageView and they are properly displayed in my UI). However when I run my app in the simulator all those images are missing. Is there a way to tell UIKit to look for those images inside this additional resource bundle in addition to the main application bundle?
I can load those images manually in viewDidLoad no problem, but I'm looking for a way to use Interface Builder to make it easier to change design/skin of those NIBs.
Update: While image files from that bundle are available right in the drop list in the IB, they won't load in runtime. However if I prefix their names with a bundle name in IB like this:
Then they will load properly in the runtime, but IB will display them as big blue question marks.
Now the question is - how to have best of the both worlds and have images displayed properly both in IB (design-time) and runtime.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了同样的问题,这可能是由于 Xcode 在组装捆绑包目标时编译图像的方式所致。由于 iOS 上不完全支持捆绑目标,因此您必须将它们创建为 Mac OS X 目标。这会留下一个有问题的构建设置,即
COMBINE_HIDPI_ARTWORK
,它将被设置为YES
。这会导致您的普通变体和@2x
变体合并到捆绑包中的单个tiff
文件中。 Mac OS 支持生成的文件,但 iOS 不支持。(感谢这个答案的提示)。
要解决此问题,请在文件导航器中选择捆绑包创建项目,选择捆绑包目标,切换到“构建设置”选项卡,然后搜索上述设置。然后选择整行并点击删除或将其设置为
NO
。那时,您应该能够在 IB 和代码中使用图像,而无需执行任何特殊操作(除了确保 .xib 和图像位于同一个包中)。
I ran into the same problem, and it's likely due to the way that Xcode is compiling your images when assembling your bundle target. Since bundle targets aren't fully supported on iOS, you have to create them as Mac OS X targets. That leaves a problematic build setting in place, namely
COMBINE_HIDPI_ARTWORK
, which will be set toYES
. This results in your normal and@2x
variants being combined into a singletiff
file in your bundle. The resulting file is supported on Mac OS but not iOS.(Thanks to this answer for the tip).
To fix this, select your bundle-creating project in the File Navigator, select your bundle target, switch to the Build Settings tab, and search for the aforementioned setting. Then either select the whole row and tap Delete or just set it to
NO
.At that point you should be able to use your images in both IB and code without doing anything special (other than making sure the .xib and image are in the same bundle).