Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asset catalog vs folder reference: When to use one or the other?

Tags:

xcode

ios

I can put files into Assets.xcassets or I can put files into folder references (the blue folders). When would I choose one over the other?

like image 697
Aaron Avatar asked Jun 30 '16 07:06

Aaron


People also ask

What is an asset catalog?

Asset catalogs simplify access to app resources by mapping between named assets and one or more files targeted for different device attributes. Attributes include device characteristics, size classes, on-demand resources, and type-specific information.

What is a folder of assets?

Assets are non-post files in the source folder, such as images, CSS or JavaScript files. For instance, If you are only going to have a few images in the Hexo project, then the easiest way is to keep them in a source/images directory. Then, you can access them using something like ! [](/images/image.

Where is the assets folder in Xcode?

General Folder Structure The asset catalog folder is at the top level of the catalog. Inside the asset catalog folder is at least one asset folder. There may also be group folders.


2 Answers

You should probably use asset catalogs as that's what Apple wants you to use going forward (the tools will reflect that) and they bring many advantages:

  • App thinning

  • Setting asset properties without code, e.g., rendering mode or slicing

  • You don't have to remember naming conventions like @2x, ~ipad, -568 etc to get device-specific assets automatically

  • Asset catalogs will point out missing assets if you tick the right boxes for the versions and devices you support, and they provide a nice overview

  • You should get used to them as some platforms (e.g., watchOS) require you to use asset catalogs

There are a few caveats:

  • If you deploy back to iOS 6, some features don't work as expected – asset catalogs still help to organize your assets, but the runtime features won't work as Xcode will just dump plain image files into your bundle.

  • If you deploy to iOS 7 or later, Xcode will compile all assets into one .car file (that's the whole idea). However, this can be harder to debug because you cannot look into the compiled file, and it also means you cannot simply get a file URL from a single asset. To create a file URL, you always have to load the asset (by its name) and write it to disk first. *

  • The last point also implies that you cannot use the NSBundle (in Swift 3.0: Bundle) APIs to retrieve URLs or paths to image files. In order to load assets from a bundle other than the main bundle, you rely on Apple to provide an API, which they do since iOS 8.0. If you organize shared code in resource bundles and deploy to iOS 7 or earlier, you shouldn't use asset catalogs. This is probably mostly relevant if you intend to develop a framework.

* E.g., the CoreSpotlight API allows you to set a thumbnailURL, but if your image is within an asset catalog, you must either write it to disk separately yourself, or use the thumbnailData property. If you had a file URL to begin with, you'd never have to load the asset into memory. I'm not sure if Spotlight could access file URLs from within your app bundle. It's just an example.

like image 153
hagi Avatar answered Nov 15 '22 20:11

hagi


You should use Assets. Many benefits the folder references can't do

1) Change color of image without any code

2) Support vector, pdf better.

3) Support Slicing image.

enter image description here

4) Manage resource easier for autolayout. If your resources has 1x, 2x, 3x and ipad image size != iphone image size, you can add 6 files into 1 asset item.

You can read more here http://krakendev.io/blog/4-xcode-asset-catalog-secrets-you-need-to-know

like image 24
Proton Avatar answered Nov 15 '22 21:11

Proton