Adding and Managing Images in SwiftUI with Xcode's Asset Catalog
In this tutorial, you'll learn how to add images to your Xcode project in SwiftUI. We'll cover how to import images to your Assets.xcassets file, as well as how to use those images in your code. Additionally, we'll go over best practices for image sizing and optimization to ensure your app performs optimally.
Adding Images to Your Xcode Project
Import images into your project, manage their appearances and variations and load them at runtime.
Create a New Image Set
An image set represents one image that you intend to load at runtime. Each image set contains multiple variations of a single image that you customize to support different device characteristics.
If you have multiple images in your app, you need to create an image set for each image.
To create an image set, generate an image asset outside of Xcode, then import it into an asset catalog. The system converts the image format into the most appropriate representation when you build the project.
- In the Project navigator, select an asset catalog: a file with a .xcassets file extension.
- Drag an image from the Finder to the outline view. A new image set appears in the outline view, and the image asset appears in a well in the detail area.
- Rename: Double-click the image set name in the outline view to rename the image set with a descriptive name, and press the Return key.
- In the Project navigator, select the asset catalog.
- In the outline view, select the image set.
- In the inspector area, select the Attributes inspector.
- In the Attributes inspector, add, remove, and edit the device characteristic settings to show additional image wells for the variations you want to customize.
- In the Project navigator, select the asset catalog.
- In the outline view, select the image set.
- In the Finder, drag other variations of the image to the wells in the detail area that match their resolutions or other characteristics.
JPG
There are four types of images that are common in iOS. JPG is great for photos that are in full size and require no transparency. For best optimizations, it is recommended to generate 1x, 2x, and 3x.
PNG
If you need transparency in your image, use the PNG. It can move on top of any background. Depending on the number of colors that you have in your image, you may have a very large file size. Just like the JPG, you'll need to export at 1x, 2x, and 3x from the design tool.
The PDF is great for glyphs and vectors that are infinitely scalable. They remain sharp regardless of the resolution. This is the preferred way since you only need a single image asset at 1x. iOS will take the scalable vector and generate the appropriate resolutions for you.
Custom Image Asset
Image("Illustration")
SF Symbols
You can also use icons from the SF Symbols library using the Image view. Without having to import anything, you have access to 2,400+ icons in different size, stroke width and colors.
Image(systemName: "xmark")
Comments
Post a Comment