Class

UIImageAsset

A container for a collection of images that represent multiple ways of describing a single piece of artwork.

Declaration

class UIImageAsset : NSObject

Overview

A common use case for UIImageAsset is the grouping of multiple images of the same item at different display scales. Image asset objects are not assigned to instances of UIImage rather; UIImage provides an asset when multiple representations of an image are available. Images retrieved from image asset catalogs using the init(named:) or init(named:in:compatibleWith:) methods automatically have an image asset object that allows access to other images from the catalog.

Registering an Image

When you register an image with an image asset, you associate a UITraitCollection object with the image. The trait collection must contain the displayScale and userInterfaceIdiom trait properties. If you do not define these traits in the trait collection, the following defaults are assigned:

For example, if you create a trait collection that only contains a horizontal size class, the default display scale and idiom are added when the image is registered.

Retrieving an Image

When you retrieve or unregister an image from an image asset, you do so using the trait collection that was used to register the image. To ensure the correct image is retrieved, the trait collection used must contain the displayScale and userInterfaceIdiom traits. If these traits are not defined in the trait collection, the following defaults are assigned:

For example, if you create a trait collection that only contains a horizontal size class, the default display scale and idiom of the current device are added when searching the UIImageAsset for an image.

UIImageView automatically retrieves the correct image when traitCollectionDidChange(_:) is called on it.

Topics

Initializing an Image Asset

init()

Creates a new image asset object.

Registering and Unregistering Images

func register(UIImage, with: UITraitCollection)

Register an image with the specified trait collection.

func register(UIImage, with: UIImage.Configuration)

Registers an image with the specified image configuration details.

Beta
func unregister(imageWith: UITraitCollection)

Unregisters the image with the specified trait collection from the image asset.

func unregisterImage(with: UIImage.Configuration)

Unregisters the image with the specified image configuration details from the image asset.

Beta

Retrieving an Image from an Image Asset

func image(with: UITraitCollection) -> UIImage

Returns the variant of the image that best matches the specified trait collection.

func image(with: UIImage.Configuration) -> UIImage

Returns the variant of the image that best matches the specified image configuration details.

Beta

Relationships

Inherits From

See Also

Assets

class NSDataAsset

An object from a data set type stored in an asset catalog.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software