从XCAsset目录中获取数据 [英] Get Data from XCAsset catalog

查看:185
本文介绍了从XCAsset目录中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,要从资产目录中获取图像,我可以使用UIImage(名称:fileName)来执行此操作。

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

然而,如果我得到了什么来自XCAsset目录的数据?我无法弄清楚这一点。

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

我试过了,

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

但它是零。我不知道如何从XCAssets目录中获取数据。如果有人可以帮助我(谷歌搜索没有帮助),请让我知道我会非常感激!

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!

更新:

为什么我要从资产目录中获取数据?我将动画gif拖入资产目录。资产目录将其解释为DATA。这就是我试图从资产目录中获取数据的原因。

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.

更新:
这是我的Assets.xcassets文件夹的屏幕截图。

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


它有一个名为_Loading的文件,在右边它被认为是数据集。
我不知道如何从资产目录中获取数据集。

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.

推荐答案

我要回答我自己的问题。

I am going to answer my own question.

从iOS 9开始,资产目录不仅仅包含图像。它们允许数据集。为了从资产目录中获取数据,您必须使用NSDataAsset类。

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.

示例:假设您有一个名为CoolJSON的数据资产

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: [])
}

在此示例中,我转身将NSData对象转换为json。

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

NSDataAsset类引用

这篇关于从XCAsset目录中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆