如何在iOS 8中使用创建和使用UIImageAsset [英] How to use create and use a UIImageAsset in iOS 8

查看:881
本文介绍了如何在iOS 8中使用创建和使用UIImageAsset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iOS 8引入了一个UIImageAsset类,其方法为 registerImage:withTraitCollection:。我该如何使用这门课程?

iOS 8 introduces a UIImageAsset class with a method registerImage:withTraitCollection:. How do I use this class?

推荐答案

通常情况下,您不必这样做。相反,您将使用资产目录。在iOS 8中,UIImageAsset只是资产目录中图像集的基础机制。

Normally, you won't have to. Instead, you'll use an asset catalog. UIImageAsset, in iOS 8, is simply the mechanism underlying image sets in asset catalogs.

例如,在iOS 8中,资产目录可以区分预期图像的版本对于不同大小类的情况,使用宽度和高度弹出菜单指定不同的大小类可能性。然后,当您使用界面中资产目录中的图像时,正确的事情会自动发生。如果我们在iPhone上将应用程序旋转到横向,并且如果图像集中有Any高度和Compact高度替代,则使用Compact高度版本。这些功能都是现场直播;如果应用程序从横向旋转到纵向,并且图像集中有任意高度和紧凑高度替代,则紧凑高度版本将替换为,并且界面中的任意高度版本自动

For example, in iOS 8, an asset catalog can distinguish between versions of an image intended for different size class situations, using the Width and Height pop-up menus to specify different size class possibilities. Then, when you use an image from an asset catalog in your interface, the right thing happens automatically. If we're on an iPhone with the app rotated to landscape orientation, and if there's both an Any height and a Compact height alternative in the image set, the Compact height version is used. And these features are live; if the app rotates from landscape to portrait, and there's both an Any height and a Compact height alternative in the image set, the Compact height version is replaced with the Any height version in your interface, automatically.

你会注意到我没有提到UIImageAsset。但是,UIImageAsset是底层机制。通过 init(命名:)从资产目录中提取图像及其图像集的名称时,其 imageAsset property是一个UIImageAsset。该图像集中的所有图像都可以通过UIImageAsset获得;每个图像都有一个与之关联的特征集合( traitCollection ),你可以通过调用向UIImageAsset询问适合特定特征集合的图像。 imageWithTraitCollection:。事实上,这正是界面正在为您做的事情。可以显示图像的界面对象在iOS 8中自动识别集合感知;它会收到 traitCollectionDidChange:消息并做出相应的响应。

You'll notice that I didn't mention UIImageAsset. However, UIImageAsset is the underlying mechanism. When an image is extracted from an asset catalog through init(named:) and the name of its image set, its imageAsset property is a UIImageAsset. All the images in that image set are available through the UIImageAsset; each image has a trait collection associated with it (its traitCollection), and you can ask the UIImageAsset for the image appropriate to a particular trait collection by calling imageWithTraitCollection:. That, in fact, is exactly what the interface is doing for you. An interface object that can display an image is automatically trait collection–aware in iOS 8; it receives the traitCollectionDidChange: message and responds accordingly.

然而,也是可以将图像组合到您自己的UIImageAsset中。在某种程度上,这就像在代码中制作资产目录(或至少是图像集)!在此示例中,我将从应用程序包中提取两个图像,并配置它们,以便在应用程序处于纵向方向时使用一个图像,而在应用程序横向显示时使用另一个图像,自动:

However, it is also possible to combine images into your own UIImageAsset. In a way, this is like making an asset catalog (or at least an image set) in code! In this example, I'll fetch two images out of the app bundle, and configure them so that one is used when the app is in portrait orientation and the other is used when the app is 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)

之后,如果将 frowney 放入t他的界面 - 例如,通过将其交给UIImageView作为其图像,或通过将其指定为UIButton的图像 - 它自动交替显示笑脸当应用程序改变方向时。

After that, if frowney is placed into the interface — for example, by handing it over to a UIImageView as its image, or by assigning it as a UIButton's image — it automatically alternates with smiley when the app changes orientation.

值得注意的是,即使没有对 frowney的持久性引用,这个魔法也会起作用 code>, smiley 或UIImageAsset(情绪。原因是系统缓存了 frowney smiley (因为调用 init(named:)),它们各自保持对它们所注册的UIImageAsset的强引用。

The remarkable thing is that this magic works even though there is no persistent reference to frowney, smiley, or the UIImageAsset (moods). The reason is that frowney and smiley are cached by the system (because of the call to init(named:)), and they each maintain a strong reference to the UIImageAsset with which they are registered.

这篇关于如何在iOS 8中使用创建和使用UIImageAsset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆