美文网首页iOS开发
使用Bundle里的资源

使用Bundle里的资源

作者: 未之 | 来源:发表于2018-05-10 16:17 被阅读274次

在构建Framework或者是Library的过程中,我们难免会使用到一些图片资源或者是xib文件,那如何管理这些资源文件,大家可能都知道把他们打包进bundle文件里,那么如何在framework内部或者外部使用到bundle里的资源文件呢,现在简要记录一下。

创建Bundle

1、创建Bundle
image.png
2、修改Build Settings里的配置

修改Base SDK

image.png

修改HIDPI

image.png

将需要的资源放进Copy Bundle Resource里

image.png
3、生成Bundle

build生成Bundle

image.png

导出Bundle

image.png

Bundle资源文件使用

Bundle里的资源文件里有图片和Xib,下面我们分两部分来分别说明如何使用

image.png
1、图片
    //方式一:

    UIImage *image = [UIImage imageNamed:@"Resource.bundle/1.png”];

    //方式二:

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Resource" ofType:@"bundle"];

    NSBundle *resourceBundle = [NSBundle bundleWithPath:path];

    image = [UIImage imageNamed:@"1.png" inBundle:resourceBundle compatibleWithTraitCollection:nil];
2、xib

因为xib最后打包后会生成一个nib文件,所以在Bundle文件夹里你看到的是一个nib文件。

如果在APP项目里的bundle里面放入一个xib文件,编译器是不会发现并编译这个xib文件并将它打包到APP里面的,所以在bundle中我们只能放nib文件来代替xib文件

使用nib文件

NSString *path = [[NSBundle mainBundle] pathForResource:@"Resource" ofType:@"bundle"];

NSBundle *resourceBundle = [NSBundle bundleWithPath:path];

UIView *testView = [resourceBundle loadNibNamed:@"TestView" owner:nil options:nil].firstObject;

testView.frame = CGRectMake(0, 0, 200, 200);

testView.backgroundColor = [UIColor redColor];

[self.view addSubview:testView];

参考:
https://blog.cnbluebox.com/blog/2014/12/08/xib-in-frameworks/
http://www.cocoachina.com/ios/20150127/11022.html

相关文章

  • 使用Bundle里的资源

    在构建Framework或者是Library的过程中,我们难免会使用到一些图片资源或者是xib文件,那如何管理这些...

  • iOS bundle资源管理

    我们下载的很多demo 里面的资源文件都是放在bundle文件里,要怎么创建使用bundle文件呢。这边又两种方法...

  • 组件化开发之如何加载图片资源

    加载图片时候 如果不使用bundle 会使组件内部的图片名和工程main.bundle图片资源出现冲突 ,如果使用...

  • 使用pathForResource:ofType取值为nil的问

    最近尝试使用pathForResource:ofType的方式去取Bundle里的资源发现没有取到,打印出来的路径...

  • NSBundle

    什么是Bundle bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代...

  • [转]iOS开发-PDF文件的读取(网络/本地)

    第一步,自然是获取pdf资源。Bundle中的资源就不说了,如果你的PDF文件都是存在于Bundle中的,那么使用...

  • swift .bundle 的详细使用

    将资源文件打包成.bundle,减轻打包的大小。 有大量的外部文件最好放在Bundle中。 创建方式: 加载使用:...

  • iOS-Bundle

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

  • swift使用Bundle读取资源

    let movieUrl=Bundle.main.url(forResource: "movie", withEx...

  • Bundle文件

    iOS - Bundle 资源文件包 iOS开发:Xcode项目添加资源文件注意点 iOS开发------使用自定...

网友评论

    本文标题:使用Bundle里的资源

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