如何在cocoapods resource_bundle中加载资源 [英] How to load resource in cocoapods resource_bundle

查看:1189
本文介绍了如何在cocoapods resource_bundle中加载资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在如何在cocoapods resource_bundle中加载资源方面遇到了很多困难。

I have struggled a lot to how to load resource in cocoapods resource_bundle.

以下是我在 .podspecs 文件中的内容。

The following is what i put in the .podspecs file.

s.source_files = 'XDCoreLib/Pod/Classes/**/*'
s.resource_bundles = {
'XDCoreLib' => ['XDCoreLib/Pod/Resources/**/*.{png,storyboard}']
}

这就是我想从主项目中做的事情。

This is what I am trying to do from the main project.

let bundle = NSBundle(forClass: XDWebViewController.self)
let image = UIImage(named: "ic_arrow_back", inBundle: bundle, compatibleWithTraitCollection: nil)
print(image)

我确实在 XDCoreLib.bundle 中看到了图片,但它返回的是零。

I did see the picture in the XDCoreLib.bundle, but it return a nil.

推荐答案

我在类似的问题上挣扎了一段时间。 pod的资源包实际上是代码所在的独立包。尝试以下方法:

I struggled with a similar issue for a while. The resource bundle for a pod is actually a separate bundle from where your code lives. Try the following:

let frameworkBundle = NSBundle(forClass: XDWebViewController.self)
let bundleURL = frameworkBundle.resourceURL?.URLByAppendingPathComponent("XDCoreLib.bundle")
let resourceBundle = NSBundle(URL: bundleURL!)
let image = UIImage(named: "ic_arrow_back", inBundle: resourceBundle, compatibleWithTraitCollection: nil)
print(image)

这篇关于如何在cocoapods resource_bundle中加载资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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