关于Bundle.我们应该在很多地方看过.比如MJ框架的下拉刷新的那个图片.都是以Bundle的形式和框架一起打包在工程里的.
如何创建一个Bundle
1.首先,我们创建一个Bundle工程
1.新建一个工程
2.创建macOS平台,Bundle类型工程
3.我们命名工程为BundleTest
2.到此为止我们得到了一个Bundle工程
4.打开工程.Bundle是红色的
根据iOS项目的经验来讲,我们需要command + B一下5.command + B后
6.showinfinder来看看Bundle的层级结构
这里其实删掉后面command + B之后还是会出现.所以我们这里先不删掉这两个没啥用的文件夹.
如何使用Bundle
从这里开始我们可以尝试往Bundle里面添加一个文件夹
我们创建一个文件夹然后拖到工程里面.注意是工程(BundleTest)里面.不是Bundle里面
千万别选错了
command + B之后Bundle就算弄好了.
使用:
//在OC中我们这么去拿image文件夹里面的图片
NSString *path = [[NSBundle mainBundle] pathForResource:@"" ofType:nil];
NSBundle *testBundle = [NSBundle bundleWithPath:path];
NSString *imagePath = [testBundle pathForResource:@"image.xxx.jpg" ofType:nil];
//在swift里面我我们这么来拿image里面的图片(假设image文件夹里面有图片)
let bundlePath = Bundle.main.path(forResource: "BundleTest.bundle", ofType: nil)!
let bundle = Bundle(path: bundlePath)!
let imagePath = bundle.path(forResource: "image/xxx.jpg", ofType: nil)!