How to make an iOS asset bundle?

57,920

Solution 1

Answer is stupidly simple

Make a folder in finder, add files to it, rename it to bundlename.bundle

drag into Xcode - success!

to access, use the form of PathToMainBundle+"/bundlename.bundle"

Solution 2

How to create a bundle

  1. Create a folder in finder.
  2. Add files to the folder
  3. Rename the folder so that its extension is .bundle (e.g. "New folder" -> "BundleName.bundle")

PS: You can at any time right click the folder and hit "Show package content" in order to add, remove or modify any of the files.

How to add the bundle to Xcode

  1. Drag it into Xcode

How to use the bundle

NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"BundleName" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath]; 
NSString *resource = [bundle pathForResource:@"fileName" ofType:@"fileType"];

(Replace BundleName, fileName and fileType with appropriate names)

Solution 3

Two other helpful bits of advice:

First, in order to see the contents of the bundle in Xcode you need to set its type in the File Inspector Utility Pane, to "Application Bundle". You still won't be able to copy to and from via Xcode. You'll need to use Terminal but Xcode will update it immediately.

Second, in order to use resources in the bundle here's a helpful snippet...

NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"AquarianHarp" ofType:@"bundle"];
NSString *imageName = [[NSBundle bundleWithPath:bundlePath] pathForResource:@"trebleclef2" ofType:@"png"];
UIImage *myImage = [[UIImage alloc] initWithContentsOfFile:imageName];

As mentioned in my comment above, you needn't actually load the bundle (you can't as it's not executable) and the ofType needs to match the case of your actual file for it to work on the device. It will work either way in simulator so don't be fooled by this red herring!

Finally, you don't need to put your resources in the "Resources" subfolder inside the bundle. It seems you can use an arbitrary layout but there may be unknown performance implications.

Solution 4

Here's how I got this to work: In Xcode create a new file | Resource | Settings Bundle. Then in the Finder select that bundle and choose Show Package Contents, and add whatever image files.

Then in the code reference an image this way:

NSString *imgName = @"bundlename.bundle/my-image.png";
UIImage *myImage = [UIImage imageNamed:imgName]; 

Solution 5

Here are the steps to create an asset or resource bundle (ex. FrameworkResources.bundle) - it's surprisingly non-obvious. This is especially useful if you are creating static frameworks.

  1. Press File -> New -> Target in Xcode
  2. Select "macOS" tab, search "Bundle"
  3. Tap "Bundle" -> click "Next" -> type product name "MyResources" -> tap "Finish"
  4. Go to "Build Settings" for the newly created Bundle. Change "Base SDK" (SDKROOT) to "iOS'
  5. Go to "Build Phases" for the newly created Bundle. Delete "Compile Sources" and "Link Binary With Libraries" (this will remove the executable within the bundle which can cause all sorts of build and app submission errors)
Share:
57,920
michael
Author by

michael

Updated on October 14, 2020

Comments

  • michael
    michael over 3 years

    I saw a custom asset bundle in an iOS project I evaluated, so at least I know it's possible.

    My issue is that I'm using a CATiledLayer with about 22,000 tiles for a given image and it takes a very long time to compile (half an hour clean build, 5-10 minutes for regular build). So, I want to take all of the images and make a custom bundle to make it portable and hopefully not recompile into the app bundle each time.

    How do I go about this? I checked the docs, but didn't see an explanation on how to actually create the bundle.