How to use the creation and use of UIImageAsset in iOS 8

IOS 8 introduces the UIImageAsset class with the registerImage:withTraitCollection: . How to use this class?

+6
source share
1 answer

Usually you donโ€™t have to. Instead, you will use the asset catalog. UIImageAsset, in iOS 8, is simply the mechanism underlying the sets of images in asset directories.

For example, in iOS 8, the asset catalog can distinguish versions of an image designed for situations of different sizes, using the Width and Height pop-up menus to indicate the different capabilities of the size class. Then, when you use the asset catalog image in your interface, the right thing happens automatically. If we are on an iPhone with the application rotated in landscape orientation, and if an alternative of any height and compact height is installed in the image, the Compact height version is used. And these functions live; if the application rotates from landscape to portrait, and there is both a height of any height and an alternative to a compact height in a set of images, the compact height version is automatically replaced by any version of height in your interface.

You will notice that I did not mention UIImageAsset. However, UIImageAsset is the main mechanism. When an image is retrieved from the asset catalog via init(named:) and the name of its image set, its imageAsset property is UIImageAsset. All images in this image set are available through UIImageAsset; each image has a collection of attributes associated with it (its traitCollection ), and you can request a UIImageAsset for the image corresponding to a particular collection of attributes by calling imageWithTraitCollection: In fact, this is exactly what the interface does for you. An interface object that can display an image will automatically recognize the collection in iOS 8; it receives the message traitCollectionDidChange: and responds accordingly.

However, it is also possible to combine images with your own UIImageAsset. In a way, this is like creating an asset catalog (or at least a set of images) in code! In this example, I will extract two images from a set of applications and configure them to be used when the application is in portrait orientation, and the other is used when the application is in landscape orientation, automatically:

 let tcdisp = UITraitCollection(displayScale: UIScreen.mainScreen().scale) let tcphone = UITraitCollection(userInterfaceIdiom: .Phone) let tcreg = UITraitCollection(verticalSizeClass: .Regular) let tc1 = UITraitCollection(traitsFromCollections: [tcdisp, tcphone, tcreg]) let tccom = UITraitCollection(verticalSizeClass: .Compact) let tc2 = UITraitCollection(traitsFromCollections: [tcdisp, tcphone, tccom]) let moods = UIImageAsset() let frowney = UIImage(named:"frowney") let smiley = UIImage(named:"smiley") moods.registerImage(frowney, withTraitCollection: tc1) moods.registerImage(smiley, withTraitCollection: tc2) 

After that, if frowney is placed in the interface - for example, passing it to UIImageView as its image or assigning it as a UIButton image - it automatically alternates with smiley when changing the orientation application.

The great thing is that this magic works, although there is no permanent link to frowney , smiley or UIImageAsset ( moods ). The reason is that frowney and smiley are cached by the system (due to the init(named:) call), and each of them maintains a strong link to the UIImageAsset with which they are registered.

+7
source

Source: https://habr.com/ru/post/974956/


All Articles