xcode - Accessing an Image with specific resolution in the Asset Catalog programmatically with Swift -
i have image set called "smileyface" contains 1x, 2x , 3x image sizes. want copy pasteboard specific size image set. how reference 1x, 2x or 3x programmatically in code below?
let image = uiimage(named: "smileyface" ) let image2 = uiimagepngrepresentation( image ) uipasteboard.generalpasteboard().setdata(image2, forpasteboardtype: "public.png")
this code copies 1x. want copy 2x image.
please not send me apple documentation not seem reference this.
update: figured out ztan's post below.
let xscale = uitraitcollection(displayscale: 3.0) //could 1.0, 2.0 or 3.0 let image = uiimage(named: "smileyface" )?.imageasset.imagewithtraitcollection(xscale) uipasteboard.generalpasteboard().image = image; println("width: \(image!.size.width).") println("trait: \(image?.traitcollection).")
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 scales.
however, dont think idea access image specific resolution. idea if 1x, 2x , 3x image set let system decide image should loaded. if want to, might change name of 1x, 2x , 3x images smileyface-small, smileyface-regular, smileyface-large.
update: func imagewithtraitcollection(traitcollection: uitraitcollection) -> uiimage
can reference image 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]))
Comments
Post a Comment