PodAsset
使用方法
为什么访问 CocoaPods 中的资源 Assets 那么痛苦?痛苦不再了!
获取该图片
// Swift
import PodAsset
let bundle = PodAsset.bundle(forPod: "SomePod")
let img = UIImage(named: "SomeDamnImage", in:bundle, compatibleWith:nil)
// Objective-C
@import PodAsset;
NSBundle* bundle = [PodAsset bundleForPod:@"SomePod"];
UIImage* img = [UIImage imageNamed:@"SomeDamnImage" inBundle:bundle compatibleWithTraitCollection:nil];
获取该数据
// Swift
let data = PodAsset.data(forFilename: "SomeData.dat", pod: "SomePod")
// Objective-C
NSData* data = [PodAsset dataForFilename:@"SomeData.dat" pod:@"SomePod"];
获取该 json
// Swift
let jsonString = PodAsset.string(forFilename: "SomeDamn.json", pod: "SomePod")
// Objective-C
NSString* jsonString = [PodAsset stringForFilename:@"SomeDamn.json" pod:@"SomePod"];
要运行示例项目,先克隆仓库,然后在 Example 目录中运行pod install
。
要求
资源捆绑名称默认为 pod 名称。如果不是,请检查第三方 pod 的 podspec 并找到资源捆绑名称
s.resource_bundles = {
'PodAsset' => ['Pod/Assets/*.png']
}
在上面的示例中,捆绑名称为 PodAsset
。
安装
PodAsset可通过对CocoaPods进行访问。安装它,只需将以下行添加到您的Podfile中。
pod "PodAsset"
如何工作
Pod Asset将搜索所有包并找到正确的包。
如果您不想使用PodAsset,您仍然可以通过bundleForClass:
找到正确的包。
如果Pod作为动态框架加载,通过以下方式定位它的包:
[NSBundle bundleForClass:[some class which is defined in your pod]]
如果Pod作为静态包加载,通过以下方式定位它的包:
[NSBundle mainBundle]
或
[NSBundle bundleForClass:[some class in your app]].
作者
许可证
PodAsset根据MIT许可证提供。有关更多信息,请参阅LICENSE文件。