Accessing an image with specific resolution in the Asset Catalog
Asked Answered
A

1

7

I have an Image Set called "SmileyFace" which contains a 1x, 2x and 3x image sizes. I want to copy to pasteboard a specific size from the image set. How do I reference the 1x, 2x or 3x programmatically in the code below?

let image = UIImage(named: "SmileyFace" )
let image2 = UIImagePNGRepresentation( image )
UIPasteboard.generalPasteboard().setData(image2, forPasteboardType: "public.png")

This code copies the 1x. I want to copy the 2x image.

I have not found anything in the Apple Documentation that seems to reference this.

Antislavery answered 22/4, 2015 at 16:49 Comment(1)
lol, I was going to suggest you to use func imageWithTraitCollection(traitCollection: UITraitCollection) -> UIImageDamiandamiani
D
6

You can use imageAsset.registerImage() method:

  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)

You can register 2x image for all the scales.

However, I dont think it is good idea to access an image with specific resolution. The idea if 1x, 2x and 3x image set is to let the system to decide which image should be loaded. If you really want to, you might change the name of your 1x, 2x and 3x images to SmileyFace-Small, SmileyFace-regular, SmileyFace-large.

UPDATE: func imageWithTraitCollection(traitCollection: UITraitCollection) -> UIImage can reference an image with specific scale:

  let image1 = image.imageAsset.imageWithTraitCollection(UITraitCollection(traitsFromCollections: [scale1x]))
  let image2 = image.imageAsset.imageWithTraitCollection(UITraitCollection(traitsFromCollections: [scale2x]))
  let image3 = image.imageAsset.imageWithTraitCollection(UITraitCollection(traitsFromCollections: [scale3x]))
Damiandamiani answered 22/4, 2015 at 17:4 Comment(3)
Thanks for response! So, this is how you add NEW images of varying scales to an image set right? I am not sure how that applies to the question. I am trying to reference an already existing 2x image inside an already existing image set?Antislavery
Thats a way to reference images from a folder not from image assets. You should not reference an image with specific resolution like 2x, 3x from image assets. If you really want to do that, you should rename 1x, 2x and 3x images to SmileyFace-Small, SmileyFace-regular, SmileyFace-large.Damiandamiani
I understand why you 'normally' would not want to reference an image with a specific resolution from an image asset. But this is not for display in the app, its for the pasteboard. I let xcode use the image asset to display in the interface when necessary for each device, yet for copying into pasteboard, I want a specific size. Thanks for your answer though, I have figured out what I need from your post. Thanks!Antislavery

© 2022 - 2024 — McMap. All rights reserved.