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

后端 未结 2 1379
日久生厌
日久生厌 2021-02-15 00:34

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?

相关标签:
2条回答
  • 2021-02-15 00:41

    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.

    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

    0 讨论(0)
  • 2021-02-15 00:58

    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.

    0 讨论(0)
提交回复
热议问题