您可以使用imageAsset.registerImage()
方法:
let scale1x = UITraitCollection(displayScale: 1.0)
let scale2x = UITraitCollection(displayScale: 2.0)
let scale3x = UITraitCollection(displayScale: 3.0)
let image = UIImage(named: "img.png")!
image.imageAsset.registerImage(UIImage(named: "img_2x.png")!, withTraitCollection: scale2x)
image.imageAsset.registerImage(UIImage(named: "img_3x.png")!, withTraitCollection: scale3x)
您可以爲所有的尺度註冊2X圖像。
但是,我不認爲這是一個好主意,訪問具有特定分辨率的圖像。如果1x,2x和3x圖像集合的想法是讓系統決定應該加載哪個圖像。如果你真的想要,你可以將你的1x,2x和3x圖像的名稱更改爲SmileyFace-Small,SmileyFace-regular,SmileyFace-large。
UPDATE: func imageWithTraitCollection(traitCollection: UITraitCollection) -> UIImage
可以使用特定規模引用圖像:
let image1 = image.imageAsset.imageWithTraitCollection(UITraitCollection(traitsFromCollections: [scale1x]))
let image2 = image.imageAsset.imageWithTraitCollection(UITraitCollection(traitsFromCollections: [scale2x]))
let image3 = image.imageAsset.imageWithTraitCollection(UITraitCollection(traitsFromCollections: [scale3x]))
笑,我會建議你使用'FUNC imageWithTraitCollection(traitCollection:UITraitCollection) - > UIImage' – ztan