100

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" : "my-asset@2x.png"
    }
  ],
  "info" : {
    "version" : 1,
    "author" : "xcode"
  }
}

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

RileyE
  • 10,874
  • 13
  • 63
  • 106

4 Answers4

142

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

RileyE
  • 10,874
  • 13
  • 63
  • 106
  • R4 versions, i.e 568 support, is there. If you view the attributes of an image set, you can select 'Device Specific' from the Devices drop down, and then you can add an R4 image, which will appear on 4" devices running iOS 7. – bandejapaisa Sep 23 '13 at 15:29
  • Is there any way to programmatically access the slicing information? Besides getting the capInsets property from the UIImage object? – Klaas Sep 27 '13 at 12:23
  • @Klaas I'm not sure why you'd need to get it by any other means than the `UIImage`. And from what I've seen, no, as each Category Image in the Group could be a completely different image, so there won't be any general information, like slicing, available. Or am I missing something? – RileyE Sep 27 '13 at 14:51
  • @RileyE I need to do some calculations for a custom overlay view and would like to use the same insets. When I solely have to create an UIImage for my calculations I would prefer to store the needed value somewhere else (and redundant). – Klaas Sep 27 '13 at 19:59
  • @Klaas In order to access that information, the OS would have to read the image, anyways. `UIImage` is just the convenience object that helps with that. In order to get the initial store, the image would have to be read at least once, even if it was handled automagically by the OS. You will just have to do it manually. – RileyE Sep 27 '13 at 20:08
  • @RileyE What I was thinking of is an API or a programmatic and save way to access the information that is stored in the underlying "Contents.json" file. – Klaas Sep 28 '13 at 00:04
  • @Klass Okay. I see what you mean. Maybe that should be a mini-project for the community of SO iOS ;) – RileyE Sep 28 '13 at 16:54
  • @RileyE `[UIImage imageNamed:@"my-button"]` it caches the image as well, which we do not always want – onmyway133 Jan 12 '15 at 16:19
  • @onmyway133 What is the issue with caching? Is it a memory issue? I've noticed it release once the variable is out of scope / released. – RileyE Jan 12 '15 at 16:53
  • 1
    The Swift code seems to be changed to `UIImage(named: imageName)` – HKTonyLee Jul 25 '16 at 08:25
  • @HKTonyLee Looks to be UIImage.init(named: imageName) – RileyE Jul 27 '16 at 00:34
29

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Yigit Yilmaz
  • 359
  • 4
  • 12
5

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.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
1

@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.

AppHandwerker
  • 1,758
  • 12
  • 22