Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I include images into a library in iOS/Xcode 4

I have an Xcode workspace with a couple of projects, say Foo and Baz, that both depend on some common code, which happens to be the RCSwitch switch button code (can be found here if anyone is interested). RCSwitch comes with a few image files that need to be loaded when Foo or Baz are running. They get loaded using the UIImage imageNamed: message, like so

UIImage *knobTmpImage = [[[UIImage imageNamed:@"btn_slider_thumb.png"] retain] autorelease];

My understanding is that for that to work the images need to be part of the application bundle. My question is how to get them there from within my library? I have tried adding the "Copy Bundle Resources" build phase to my library project and then adding all the images to that without any effect. The images do not get found at runtime.

Then I tried adding the images to the "Copy Bundle Resources" build phase of Foo and Baz themselves; one has to select "Add Others" as the upcoming dialog does not list any resources outside the current project. That actually works, but it does seem like a weird way of doing it. Neither Foo nor Baz ought to have knowledge of the internals of the library. What if I wanted to ship the library to external customers?

Is there another way of doing this? Is it even possible to have images or other resources in a libXXX.a file?

like image 303
McKrassy Avatar asked Jul 28 '11 23:07

McKrassy


1 Answers

What I do for this is have my static library create a bundle which I include in the main app project. If using a sub project / sibling project in a workspace then this bundle can be added in such a way that if you edit the library project to add another image then it gets rebuilt meaning the main app project picks up the change.

I wrote a tutorial about it here - http://www.galloway.me.uk/tutorials/ios-library-with-resources/

The bit you'll be interested in is the category on UIImage which I use to pick up the images in the library bundle. So from the library's code I use the method added in the category to grab the image from the right bundle.

like image 73
mattjgalloway Avatar answered Nov 15 '22 09:11

mattjgalloway