TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

How to load assets from Xcode asset catalogs

Swift version: 5.10

Paul Hudson    @twostraws   

Xcode asset catalogs are a smart and efficient way to bring together your artwork in a single place. But they are also optimized for performance: when your app is built, your assets converted to an optimized binary format for faster loading, so they are recommended for all kinds of apps unless you have a specific reason to avoid them. (Note: SpriteKit games should texture atlases if possible.

If you don't already have an asset catalog in your project, you can create one by right-click on your project and choosing New File. From "iOS" choose "Resource" then Asset Catalog, then click Next and name your catalog. You can now select your new asset catalog in Xcode, and drag pictures directly into it.

Images stored inside asset catalog all retain their original filename, minus the path extension part. For example, "taylor-swift.png" will just appear as "taylor-swift" inside your asset catalog, and that's how you should refer to it while loading too.

Asset catalogs automatically keep track of Retina and Retina HD images, but it's recommended that you name your images smartly to help make the process more smooth: taylor-swift.png, taylor-swift@2x.png and taylor-swift@3x.png are the best way to name your files for standard, Retina and Retina HD resolutions respectively.

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Available from iOS 7 – see Hacking with Swift tutorial 2

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 2.6/5

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.