美文网首页
iOS开发 framework内加载xib及图片资源

iOS开发 framework内加载xib及图片资源

作者: Leoeoo | 来源:发表于2021-04-22 16:24 被阅读0次

本文介绍如何在framework内加载xib/图片及区别。

一.app目录介绍

1.编译后的目录
image.png
2.app根目录
image.png image.png image.png

静态库.framework相当于是一个bundle,里面可以放代码,也可以放图片、xib等资源文件。如果放了图片、xib等资源文件,需要在Build Phases --> Copy Bundle Resources中添加该静态库.framework。

image.png
3.动态库子目录
image.png
image.png

二.bundle介绍

  • 无论是静态库或者动态库使用[NSBundle mainBundle]获取的是app根目录。
  • 静态库使用[NSBundle bundleForClass:[self class]]获取的是app根目录。
  • 动态库使用[NSBundle bundleForClass:[self class]]获取的是当前动态库目录。
  • 静态库就是一个bundle,只不过里面可以多了编译后的二进制文件。

三.动态库(dynamic framework)内加载图片/xib

由于动态库在app加载时,会创建单独的文件夹,如上图为:Frameworks/FHDynamicFramework.framework:
故需要使用以下方式:

NSBundle *bundle = [NSBundle bundleForClass:[self class]];
UIImage *image = [UIImage imageNamed:@"dynamic.png" inBundle:bundle compatibleWithTraitCollection:nil];

四.静态库.a内加载图片/xib

创建bundle(FHStaticFrameworkBundle.bundle),并把需要加载的图片放到bundle内,把bundle放入到项目中,代码如下:

NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"FHStaticFrameworkBundle.bundle" ofType:nil];
NSBundle *bundle3 = [NSBundle bundleWithPath:bundlePath];
UIImage *image = [UIImage imageNamed:@"static.png" inBundle:bundle3 compatibleWithTraitCollection:nil];
image.png

五.静态库.frameowk内加载图片/xib

// 以下两种方式都可以
//    UIImage *image = [UIImage imageNamed:@"static.png"];
NSBundle *bundle = [NSBundle mainBundle];
UIImage *image = [UIImage imageNamed:@"static.png" inBundle:bundle compatibleWithTraitCollection:nil];
return image;

详见我的另一篇文章:iOS开发 xib使用

Demo:https://gitee.com/ayangcool100/fhtest-framework-resource.git

相关文章

网友评论

      本文标题:iOS开发 framework内加载xib及图片资源

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