When instatiating a UIImage
in code we usually write something like this:
Easy enough, but often times we need to reuse the same image multiple times in our code base. Copying and pasting the same initializer over and over again is painful and error prone. This post explores a way to improve this by using a Swift property wrapper.
How about wrapping it in a struct and making nice contants?
This is already much better. We now have a safe way to retreive the image and we even get autocomplete support. For simple code bases this is enough and good to go.
Frameworks and Bundles
But what if we are working in a modularized code base where the image assets are embedden in a frameworks? Instatiating an image now becomes this:
It gets even more involved when the images are embedded in an asset bundle within the framework:
ImageAssetRetreivable Property Wrapper
Copy/pasting the above block of code each time you need that image is an absolute no-go. And although it is absolutley possible to create a nice struct with traditional Swift, I find it so much more elegant to solve this with a property wrapper.
For a great introduction to Swift property wrappers have a look at John Sundell’s article Property Wrappers in Swift.
Let’s break this down a bit.
- We need a way to access the asset bundle. This extension to
Bundle
does just that:- Get the framework bundle
- get the url of the asset bundle
- get the asset bundle
-
The actual property wrapper. It is initialized with the name of the image in the asset catalog. The wrapped value is the
UIImage
instantiated from the image name and the asset bundle. I am not entirely sure why prefixing withUIKit.
is necessary here, but the compiler otherwise complains. - A struct containing all the images we want to use. Adding a new image is as simple as creating a new static var and annotating it with the property wrapper, passing the image name.
Usage:
Conclusion
Property wrappers are a great addition to Swift and, if used carefully, can make your code easier to read, safer and more elegant. The approach presented in this post can also be translated to other assets in asset catalogs like colors.
Let me know what you think. You can contact me either via Twitter or email.