美文网首页
iOS-Bundle

iOS-Bundle

作者: GX_Dust | 来源:发表于2018-06-15 09:39 被阅读8次

什么是Bundle?

Bundle就是资源文件包, 将我们使用的Xib, 图片, 其他文件组织在一起! Bundle是不参与编译的, 也就是它里面没有可执行的文件, Bundle只是资源. 编译后可能存储在内存中的数据区!

怎么创建Bundle?

1. 第一步

就好比你要创建另一个项目了
iOS里面没有, macOS里面才有 来个名字! Finish

2. 第二步

这是Bundle出来了, 默认是macOS的 改成iOS的, 就是使用时候你的版本不能高于这个版本

3. 其他步, 设不设置没关系

如果设置为YES, 编译时后只生成当前的机器架构, bundle中改不改, 没什么作用 听说为YES时候, Bundle里面的图片变为tiff, 试了一下, 不改也没什么事 安装相关配置, YES代表安装, 安装路径填在 installation Directory里面, Bundle无需安装配置, 仅仅是个资源, 我试了一下, 安不安装不影响

4. 第四步, 添加文件, com + B

确定文件在Bundle里面!!!!!!!!!!!!!!!!!

使用创建的Bundle?

创建一个项目, 确保将上面的bundle加入
  1. 取这个bundle的文件
    (1)图片
// 一定要确定好路径, 这个图片是在MainBundle里面的Bundle
UIImage *image = [UIImage imageNamed:@"GXBundle.bundle/QQ20180613-142252.png"];
// 返回路径
NSString *path = [[NSBundle mainBundle] pathForResource: @"GXBundle.bundle" ofTYpe: @"前面写了扩展名, 后面不写"];
NSBundle *bundle = [NSBundle bundleWithPath:path];
//加载图片又学了一个方法, 带个Bundle
UIImage *image = [UIImage imageNamed:@"QQ20180612-205946.png" inBundle:bundle compatibleWithTraitCollection:nil];

(2)Xib

//知道你的Bundle放哪里了, 直接来个路径
NSString * pathStr = [[NSBundle mainBundle] pathForResource:@"GXBundle.bundle" ofType:nil];
--这个是拼路径
NSString *mainBundlePath = [[NSBundle mainBundle] resourcePath] ;//主Bundle
NSString *pathStr = [mainBundlePath stringByAppendingPathComponent:@"GXBundle.bundle"];//拼路径, 找我的
--
//通过path(还有URL的)加载你的Bundle
NSBundle *bundle = [NSBundle bundleWithPath:path];
//掉Bundle的方法, 返回个数组, 确切的说, 这个bundle的分类方法
UIView *myView = [[bundle loadNibNamed:@"testXibView" owner:nil options:nil] lastObject];

//通过Nib
UINib *nib = [UINib nibWithNibName:@"testXibView" bundle:bundle];
UIView *myView = [[nib instantiateWithOwner:nil options:nil] lastObject];

注意: 用真机编译Bundle, 需要关闭 bitCode

相关文章

  • iOS-Bundle

    什么是Bundle? Bundle就是资源文件包, 将我们使用的Xib, 图片, 其他文件组织在一起! Bundl...

网友评论

      本文标题:iOS-Bundle

      本文链接:https://www.haomeiwen.com/subject/zdqjeftx.html