zoukankan      html  css  js  c++  java
  • iOS

    1、Bundle 文件

    Bundle 文件,就是资源文件包。我们将许多图片、XIB、文本文件组织在一起,打包成一个 Bundle 文件。方便在其他项目中引用包内的资源。

    Bundle 文件是静态的,也就是说,我们包含到包中的资源文件作为一个资源包是不参加项目编译的。也就意味着,bundle 包中不能包含可执行的文件。它仅仅是作为资源,被解析成为特定的二进制数据。

    2、制作 Bundle 文件

    1、新建 Bundle 项目

    创建名为 yooweiSourcesBundle(最后要生成的 Bundle 文件名称)的工程,注意 Bundle 默认是 macOS 系统的,Xcode 高版本中需要在macOS => Framework & Library 选项下找到。具体创建路径如下:

    打开Xcode, 选择File ----> New ---> Project,选择macOS ----> Framework & Library ---> Bundle。

    按照引导一步一步来,最后选择存放的位置,那么一个一个bundle就创建好了。

    补充注意:如果已经在静态库工程中,想新增一个资源bundle的话,需要按照如下图所示的方式进行

    2、修改 Bundle 配置信息

    (1)因为 Bundle 默认是 macOS 系统的,所以需要修改他的信息,修改成 iOS 系统。

    (2)设置 Build Setting 中的 COMBINE_HIDPI_IMAGES 为 NO,否则 Bundle 中的图片就是 tiff 格式了。

    3、可选配置

    作为资源包,仅仅需要编译就好,无需安装相关的配置,设置 Skip Install 为 NO。同样要删除安装路径 Installation Directory 的值。

    该资源包的 pch 文件和 strings 文件是可以删除(老版本Xcode会有,高版本没有)。

    4、添加文件

    将资源文件或文件夹拖动到工程中的 SourcesBundle 文件夹下面。

    5、编译生成 Bundle 文件

    我们分别选择 Generic iOS Device 和任意一个模拟器各编译一次,编译完后,我们会看到工程中 Products 文件夹下的 SourcesBundle.bundle 由红色变成了黑色。

    然后 show in finder,看看生成的文件。我们看到它为真机和模拟器都生成了 .bundle 资源文件。

    选中 .bundle 文件右键 显示包内容,我们可以看到之前拖拽到工程中的资源文件都在其中。

    3、使用 Bundle 文件

    打开Xcode, 选择File ----> New ---> Project,选择iOS ----> Application ---> single View App  创建一个yooweiLiveDemo 用来测试bundle里面文件的使用。将生成的真机(Debug-iphoneos)Bundle 资源文件拖拽到需要使用的工程中。

    1、加载 Bundle 中的 xib 资源文件

    -(void)useXIB{

    // 设置文件路径

    //    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"yooweiSourcesBundle" ofType:@"bundle"];

    //    NSBundle *resourceBundle = [NSBundle bundleWithPath:bundlePath];

        NSBundle *resourceBundle=[NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"yooweiSourcesBundle" withExtension:@"bundle"]];

        // 加载 nib 文件

        UINib *nib = [UINib nibWithNibName:@"WYLiveCell" bundle:resourceBundle];

        NSArray *viewObjs = [nib instantiateWithOwner:nil options:nil];

        // 获取 xib 文件

        UIView *view = viewObjs.lastObject;

        view.frame = CGRectMake(20, 50, self.view.bounds.size.width - 40, self.view.bounds.size.width - 40);

        [self.view addSubview:view];

    }

    2、加载 Bundle 中的图片资源文件

    -(void)useImage{

    //   1 指定绝对路径的形式

        UIImage *imageOne = [UIImage imageNamed:@"yooweiSourcesBundle.bundle/loading_yoowei.png"];

        UIImageView *one=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 30, 30)];

        one.image=imageOne;

        [self.view addSubview:one];

    //    拼接路径的形式

        NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"yooweiSourcesBundle" ofType:@"bundle"];

        NSString *imgPathTwo= [bundlePath stringByAppendingPathComponent:@"loading_yoowei"];

        UIImage *imageTwo = [UIImage imageWithContentsOfFile:imgPathTwo];

        UIImageView *two=[[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 30, 30)];

        two.image=imageTwo;

        [self.view addSubview:two];

    //    宏定义的形式

    #define MYBUNDLE_NAME   @"yooweiSourcesBundle.bundle"

    #define MYBUNDLE_PATH   [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:MYBUNDLE_NAME]

    #define MYBUNDLE        [NSBundle bundleWithPath:MYBUNDLE_PATH]

        NSString *imgPathThree= [MYBUNDLE_PATH stringByAppendingPathComponent:@"loading_yoowei"];

        UIImage *imageThree = [UIImage imageWithContentsOfFile:imgPathThree];

        UIImageView *three=[[UIImageView alloc]initWithFrame:CGRectMake(90, 90, 30, 30)];

        three.image=imageThree;

        [self.view addSubview:three];

    }

     3、加载其他资源文件

    -(void)uesOther{

    //    再比如获取静态库里面bundle里面的资源文件证书

        NSString *pathStr = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"yooweiSourcesBundle.bundle"];

        NSBundle *pathBundle = [NSBundle bundleWithPath:pathStr];

        //获取Bundle里的资源路径

        NSString *cacerPath = [pathBundle pathForResource:@"ca" ofType:@"cer"];

        NSData *cerDate=[NSData dataWithContentsOfFile:cacerPath];

        NSLog(@"%@",cerDate);

    }

    效果如下:

  • 相关阅读:
    Python列表List增删改查、拷贝
    面向对象之继承——python篇
    面向对象——python篇
    异常——python基础篇
    函数进阶
    文件操作——python基础篇
    函数(一)——python基础篇
    字典——python基础篇
    特有的字符串格式化函数format——python篇
    webdriver对应谷歌版本下载地址
  • 原文地址:https://www.cnblogs.com/richard-youth/p/7744759.html
Copyright © 2011-2022 走看看