Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Both Referencing an Asset Catalogue and Including it from a Framework Create Duplication?

Tags:

As discussed here & here, the only apparent way to host re-usable images in a Framework's .xcassets file, then use those images in the storyboard / XIB of an app, is to manually create a reference to that catalog file directly within the app's project. This puts the assets within the app's main bundle, and avoids this error when trying to use them via Interface Builder:

Could not load the "ImageName" image referenced from a nib in the bundle with identifier "BundleName"

My question, though, is: Does this lead to duplication of the assets at build-time? Is iOS smart enough not to double-up assets which are both:

  • Referenced in the app's main bundle and
  • Included in the bundle of an embedded framework

If this does lead to duplication, what other approach can achieve the same result more efficiently? The framework needs to be embedded in the app, because it contains other reusable code, and the assets need to be in the framework's catalog, because they're also used in other apps.

like image 898
TheNeil Avatar asked Apr 26 '19 01:04

TheNeil


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 asset catalog in Swift?

Asset catalogs help you quickly organize and manage your app's resources. In an asset catalog, each asset set represents one resource — like an image, color, or data file — that your app loads at runtime.

What is asset catalog Xcode?

An asset catalog, simply put, is a single folder in Xcode that you use to organize your app's images, icons, colors, and more. Instead of adding individual images to Xcode's file organizer, you add assets neatly organized in a single catalog.


1 Answers

Apple's goal is to minimize the space required for assets, so assets are combined and compressed into one file called 'Assets.car'.

However as correctly noted in the comments, the image is included twice in the final .ipa, once in the Assets.car file of the app and once in the Assets.car file of the framework.

When you submit to the app store a process called App Thinning is started that creates variants for different actual devices.

You can mimic the process locally and with a third-party tools even take a look inside the 'Asset.car' file.

I described the whole process in detail in this answer: Pixelated images on iOS10 when building with Xcode 10

To test it myself I did the following:

  • I created a workspace
  • I added a framework
  • I added an app
  • inside the framework I added a 'Media.xcassets'
  • I reference this Media.xcassets from the app
  • I added one picture called 'Regensburg.jpg' to Media.xcassets
  • I use this image in a storyboard

There are two Assets.car files, one in the base folder of the app and one in the framework. If you take a look with the third-party tool, you can see that both Assets.car files contain the same image.

So finally the image is saved twice in the .ipa file.

Conclusion

Unfortunately, it looks like if you want to avoid this duplication, you can't refer to it directly in the storyboard, but have to retrieve the image programmatically from the framework.

Screenshot

screenshot

In the screenshot you can see:

  • Xcode project structure with framework and app as well as .xcassets in the background

  • usage of the only image inside in the storyboard

  • the two extracted Assets.car files in the Finder in the lower right area (once in the application folder and once in the framework)

  • finally the mentioned third-party tool in the lower left area which shows the embedded image

Conclusion Unfortunately, it looks like if you want to avoid this duplication, you can't use Interface Builder and instead have to retrieve the image programmatically from the framework.

More information

Like in my other answer I would recommend to take a look at this WWDC 2018 video: Session 227, Optimizing App Assets, https://developer.apple.com/videos/play/wwdc2018/227/

like image 175
Stephan Schlecht Avatar answered Nov 15 '22 07:11

Stephan Schlecht