Access Asset Catalog programmatically

IosObjective CXcodeAssets

Ios Problem Overview


I know it's a new feature and this may not be possible, but I would love to be able to use an Asset Catalog to organize my assets, but I access all of my images programmatically. How would I access my images, now? Do I still access them by their file names like so:

[UIImage imageNamed:@"my-asset-name.png"];

Seemingly, the Asset Catalog doesn't reference the extension, so would it be more efficient to access it without the ".png"?

The reason I am asking instead of testing for myself is that even after removing my assets and Asset Catalog, then cleaning the build folder, I can still access my assets in my application. This is preventing me from testing the Asset Catalog, when I implement it.

After looking through the Asset Catalog, I found the "Contents.json" for each asset and it's formatted like so:

{
  "images" : [
    {
      "idiom" : "universal",
      "scale" : "1x"
    },
    {
      "idiom" : "universal",
      "scale" : "2x",
      "filename" : "[email protected]"
    }
  ],
  "info" : {
    "version" : 1,
    "author" : "xcode"
  }
}

I'm still unsure of how I should be accessing it, but maybe this will help?

Ios Solutions


Solution 1 - Ios

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 @"[email protected]" 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).

Solution 2 - Ios

Also Apple has added new way to get image from assets with Swift 3, It is calling as 'Image Literal' and work as below:

Image Literal

Solution 3 - Ios

Swift

You can get a reference to an image in your asset catelog with

UIImage(named: "myImageName")

You don't need to include the extension.

Solution 4 - Ios

@RileyE is 100% right. However, from my experiences It's also worth noting that sometimes the asset catalogue reference for the image can contain trailing white space. You probably wouldn't notice if using storyboards/xibs as the auto complete will add it. But when you reference it from code it's not so obvious what the problem is.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRileyEView Question on Stackoverflow
Solution 1 - IosRileyEView Answer on Stackoverflow
Solution 2 - IosYigit YilmazView Answer on Stackoverflow
Solution 3 - IosSuragchView Answer on Stackoverflow
Solution 4 - IosAppHandwerkerView Answer on Stackoverflow