How to make an iOS asset package?

I looked at my own asset package in an iOS project that I appreciated, so at least I know this is possible.

My problem is that I use CATiledLayer with about 22,000 fragments for this image, and it takes a lot of time to compile (half an hour of clean assembly, 5-10 minutes for regular assembly). So, I want to take all the images and make my own set to make it portable and, hopefully, not to recompile into the application package every time.

How should I do it? I checked the docs but didn’t see an explanation of how to actually create the package.

+52
ios bundle assets
Feb 03 '11 at 15:41
source share
6 answers

The answer is stupidly simple

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

drag and drop in Xcode - success!

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

+88
Mar 11 '11 at 19:32
source share

How to create a package

  • Create a folder in finder.
  • Add files to folder
  • Rename the folder so that its extension is .bundle (for example, "New Folder" → "BundleName.bundle")

PS: you can right-click on the folder at any time and click "Show Package Contents" to add, delete or modify any files.

How to add a package to Xcode

  • Drag it to Xcode

How to use the package

 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 the appropriate names)

+34
May 27 '14 at 8:26
source share

Two other helpful tips:

First, in order to see the contents of a package in Xcode, you need to set its type in the File Inspector utility panel, in the "Application Bundle". You still cannot copy using Xcode. You will need to use Terminal, but Xcode will immediately update it.

Secondly, to use the resources in the package here is a useful 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 really do not need to download the package (you cannot, because it is not executable), and ofType must match the case of your actual file for it to work on the device. This will work in the simulator, so do not be fooled by this red herring!

Finally, you don’t need to place your resources in the “Resources” subfolder inside the package. It seems you can use a custom layout, but the performance implications may not be known.

+25
Nov 19 '11 at 12:50
source share

Here's how I got it to work: in Xcode, create a new file | Resource | Bundle Settings. Then in Finder select this package and select "Show Package Contents" and add all image files.

Then, in the link to the code, the image as follows:

 NSString *imgName = @"bundlename.bundle/my-image.png"; UIImage *myImage = [UIImage imageNamed:imgName]; 
+10
Jul 10 '11 at 5:17
source share

My notes on linking and reading files in an Xcode project

Actions:

  • Create a test.txt file and add the text "testing" into it, and then put it in a folder called test.bundle
  • Drag it next to your .app file in xcode (copy)
  • print(Bundle.main.resourcePath!+"/temp.bundle/test.txt") Result: print(Bundle.main.resourcePath!+"/temp.bundle/test.txt") /Resources/temp.bundle/test.txt

Example:

 print(content(Bundle.main.resourcePath!+"/temp.bundle/test.txt"))// testing✌️ static func content(_ path:String)->String?{ do { let content = try String(contentsOfFile:path, encoding:String.Encoding.utf8) as String//encoding: NSUTF8StringEncoding return content } catch { return nil } } 
0
Jul 31 '17 at 20:41
source share

Here are the steps to create an asset or resource package (for example, FrameworkResources.bundle ) - this is surprisingly unobvious. This is especially useful if you are creating static frames.

  1. Click File → New → Target in Xcode
  2. Select the tab "macOS", search for "Bundle"
  3. Click “Package” → click “Next” → enter the name of the product “MyResources" → click "Finish"
  4. Go to Build Settings for the newly created Package. Change “Base SDK” ( SDKROOT ) to “iOS”
  5. Go to Build Phases for the package you just created. Delete "Compilation Sources" and "Link Binary Files to Libraries" (this will delete the executable file in the bundle, which can cause all kinds of errors in building and sending the application)
0
Feb 05 '19 at 17:04
source share



All Articles