Programmatically Access Image Assets

Access Asset Catalog programmatically

In order to access the image from the Asset Catalog, you only need to access the name of the asset group without any extensions.

So, if you add an image named @"my-button@2x.png" to the Asset Catalog, it will create an asset group called my-button.

Now, all you have to do is access the image like so:

// Objective-C
[UIImage imageNamed:@"my-button"];
// Swift
UIImage(named: "my-button")

Also, you can edit the asset group by renaming it (without renaming the images) or changing it's individual components. This will allow you to follow easier naming conventions as well as show completely different assets between different UIScreen scales without any scale checks.

In order to incorporate images for different device sizes, you may need to toggle it under the "Devices" subheading in the Asset Catalog Group's options. Here is an example of that toggle (available by right clicking the group).

How to access Image Assets like an array in Swift

Then don't use image literal. Image literals are just that - a hard value in code that you can't change during run time. Load an image dynamically from your bundle:

if let image = UIImage(named: "myImage" + userChoice) {
self.But_Settings.setImage(image, for: .normal)
}

Figuring assets.xcassets path programmatically using SWIFT

If you only need the path to the file (and not an instance of UIImage), this will do it:

Swift 2:

if let resourcePath = NSBundle.mainBundle().resourcePath {
let imgName = "dog.png"
let path = resourcePath + "/" + imgName
}

Swift 3/4:

if let resourcePath = Bundle.main.resourcePath {
let imgName = "dog.png"
let path = resourcePath + "/" + imgName
}

Accessing an image with specific resolution in the Asset Catalog

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]))

How to load specific image from assets with Swift

You cannot load images directly with @2x or @3x, system selects appropriate image automatically, just specify the name using UIImage:

UIImage(named: "green-square-Retina")

Load an image from assets folder

If you know the filename in the code, calling this won't be a problem:

ImageView iw= (ImageView)findViewById(R.id.imageView1);  
int resID = getResources().getIdentifier(drawableName, "drawable", getPackageName());
iw.setImageResource(resID);

Your filename will be the same name as drawableName so you won't have to deal with assets.



Related Topics



Leave a reply



Submit