14

I understand that to get images from the asset catalog i can use UIImage(named: "fileName") to do it.

however, what if i am getting DATA from the XCAsset catalog? I can't figure this out.

I have tried,

let url = NSBundle.mainBundle().URLForResource("fileName", withExtension: nil)
let data = NSData(contentsOfURL: url!)

But it is nil. I do not know how to get data from the XCAssets catalog. If anyone can help me out (googling hasn't helped) please let me know I will be very thankful!

Update:

Why am I trying to get data from an asset catalog? I dragged an animated gif into the asset catalog. And the asset catalog interprets that as DATA. This is why I am trying to get data out of the asset catalog.

Update: This is a screen shot of my Assets.xcassets folder looks like.

image It has a file called "_Loading" and on the right it is considered a "Data set". I'm not sure how to get the data set from the Assets catalog.

DerrickHo328
  • 4,054
  • 6
  • 24
  • 48
  • You don't get data from an asset catalog. Asset catalogs have images, not data. The code you posted is for reading a file from the resource bundle, not an asset. – rmaddy Oct 26 '15 at 16:46
  • How and why do you put data into XCAssets? – zaph Oct 26 '15 at 16:46
  • What problem are you having with the code you posted? In general it is correct for reading a file from the resource bundle. Just make sure your specify the proper filename and extension. And remember that case matters. – rmaddy Oct 26 '15 at 16:47
  • It works if "fileName" is not in the asset catalog. I put data into the asset catalog since that is a new feature. I can drag the data into the asset catalog but pulling it out is a mystery. – DerrickHo328 Oct 26 '15 at 17:47
  • Btw... I dragged an animated gif into the asset catalog. And the asset catalog interprets that as DATA. This is why I am trying to get data out of the asset catalog. – DerrickHo328 Oct 26 '15 at 17:58

2 Answers2

65

I am going to answer my own question.

Since iOS 9, the Asset Catalog allows more than just Images. They allow Data sets. In order to get data from the Asset Catalog, you must use the NSDataAsset class.

Example: Assume you have an Data Asset named "CoolJSON"

if let asset = NSDataAsset(name: "CoolJSON") {
    let data = asset.data
    let d = try? NSJSONSerialization.JSONObjectWithData(data, options: [])
}

In this example I turned an NSData object into a json.

NSDataAsset class reference

DerrickHo328
  • 4,054
  • 6
  • 24
  • 48
  • Cool. I did't know about it. – Serge Maslyakov Oct 27 '15 at 14:45
  • 1
    there is a special configuration i need to do?, because when i check for the data asset it returns nil therefore i cant access the content. – Klinkert0728 Jan 28 '16 at 19:16
  • @Klinkert0728 no special configuration. What data set are you trying to get? – DerrickHo328 Jan 28 '16 at 19:24
  • i created one DataAsset "airports" in my .xcassets and it contains a json, but when y call the method NSDataAsset(name: "airports") it returns nil – Klinkert0728 Jan 28 '16 at 19:31
  • @Klinkert0728 it should be NSDataAsset(name: "jsonName") – DerrickHo328 Jan 28 '16 at 19:32
  • yes, the "jsonName" is airports too, i created a new project and did the same thing and it worked, i can't understand which is the problem in the project – Klinkert0728 Jan 28 '16 at 19:38
  • @Klinkert0728 ok so your saying it works in a fresh project but not in your current project? Well if you made a brand new xcasset catalog it's possible that you may not have added that to your project. Therefore it may be only accessing the default one. This is only speculation. If you have the code on github I can try taking a look. – DerrickHo328 Jan 28 '16 at 19:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/101929/discussion-between-klinkert0728-and-calimari328). – Klinkert0728 Jan 28 '16 at 19:54
  • is there any way to save / add more data to that json file in assets ? – Adnan Munir SE Feb 26 '16 at 22:43
  • In assets right click the json and view it in finder. Open and edit it. – DerrickHo328 Feb 26 '16 at 22:44
  • @Klinkert0728 The issue was likely that the Deployment Target of your product was too old for the feature which requires iOS 9 / macOS 10.11. – Kentzo Jan 31 '19 at 01:17
-1

Load gif image from Assets.xcassets file. That`s same for loading json files.

  1. NEW: 【New Data Set】 crate a data set and rename "funny" in Assets.xcassets folder. all right, the gif file is added to Assets.xcassets folder. enter image description here

  2. USE:

    NSDataAsset *asset = [[NSDataAsset alloc] initWithName:@"funny"]; 
    self.gifImageView.image = [UIImage sd_animatedGIFWithData:asset.data];                                        
    
    • (UIImage *)sd_animatedGIFWithData:(NSData *)data: is a UIImage+GIF category for loading gif image in SDWebImage.
Beyond Chao
  • 119
  • 1
  • 5