Class

UIImageAsset

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

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()

Initializes the image asset.

Registering and Unregistering Images

func register(UIImage, with: UITraitCollection)

Associates an image with the trait collection and registers the image to an image asset.

func unregister(imageWith: UITraitCollection)

Removes the image associated with the trait collection from an image asset.

Retrieving an Image from an Image Asset

func image(with: UITraitCollection)

Returns the image that is registered to an image asset with a specified trait collection.

Relationships

Inherits From

See Also

Assets

class NSDataAsset

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