简体   繁体   English

iOS:如何在Interface Builder中使用自定义包中的图像?

[英]iOS: How to use images in custom bundle in Interface Builder?

I have a bundle where i put images in it. 我有一个放在其中的图像包。

The contents are: 内容是:

MyBundle.bundle/images/picture1.png
MyBundle.bundle/images/picture2.png

I dragged MyBundle.bundle into my project. 我将MyBundle.bundle拖到我的项目中。

Now I can see these images in Interface Builder and can even use them. 现在,我可以在Interface Builder中看到这些图像,甚至可以使用它们。 However, when I run the app, I don't see the images. 但是,当我运行该应用程序时,没有看到图像。

What's wrong? 怎么了?

Thanks, 谢谢,

Make sure the bundle is getting copied over in the "Build Phases" of the project settings for the target. 确保在目标的项目设置的“构建阶段”中复制了捆绑包。

Also, try setting them progrmatically and see if they show: 另外,尝试以编程方式设置它们,看看它们是否显示:

myImageView.image = [UIImage imageNamed:@"MyBundle.bundle/images/picture1.png"];

I just tried this in my project and found that what you need to do is specify the image with the bundle name in front like this: 我只是在我的项目中尝试过此操作,发现您需要做的是指定图像,其束名在前面,如下所示:

IB中的图像

In the IB the image will look broken: 在IB中,图像看起来很破损:

破碎

but when you build and run your project the image will show correctly . 但是当您构建运行项目时,图像将正确显示

So, in your case, use MyBundle.bundle/images/picture1.png in the actual Interface Builder Image box. 因此,在您的情况下, MyBundle.bundle/images/picture1.png在实际的Interface Builder图像框中使用MyBundle.bundle/images/picture1.png

Also, when you dont put the bundle name in front, you get this warning when you load the view with the bundle image: 另外,当您不将包名称放在前面时,使用包图像加载视图时会收到以下警告:

2011-10-12 08:22:16.360 UTDebug[721:11c03] Could not load the "map.png" image referenced from a nib in the bundle with identifier "com.companyname.myproject"

You need to put the XIB in the bundle, too. 您还需要将XIB放入捆绑包中。 When you do that, resources for the interface will be relative to the bundle, so they'll work both in IB and when you load the interface in your app. 当您这样做时,界面的资源将相对于包,因此它们将在IB中以及您在应用程序中加载界面时均可用。

You might have a stray build setting from having created the bundle as a Mac OS X target. 通过将捆绑软件创建为Mac OS X目标,您可能会有一个混乱的构建设置。

See my other answer for the solution. 看到我的其他答案的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM