Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

Now I can see these images in Interface Builder and can even use them. However, when I run the app, I don't see the images.

What's wrong?

Thanks,

like image 770
Van Du Tran Avatar asked Oct 11 '11 23:10

Van Du Tran


People also ask

How do I add images to Xcassets?

In the Project navigator, select an asset catalog: a file with a . xcassets file extension. Drag an image from the Finder to the outline view. A new image set appears in the outline view, and the image asset appears in a well in the detail area.

What is IOS Interface Builder?

Interface Builder is a software development application for Apple's macOS operating system. It is part of Xcode (formerly Project Builder), the Apple Developer developer's toolset. Interface Builder allows Cocoa and Carbon developers to create interfaces for applications using a graphical user interface.


2 Answers

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:

Image in IB

In the IB the image will look broken:

Broken

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.

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" 
like image 189
chown Avatar answered Oct 01 '22 07:10

chown


You need to put the XIB in the bundle, too. 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.

like image 38
Glenn Maynard Avatar answered Oct 01 '22 08:10

Glenn Maynard