iOS库(四)资源文件(图片)

2023-04-10  本文已影响0人  fanren

前言

Framwork中包含了本地图片的读取和使用时,直接使用[UIImage imageNamed:...]是没有办法找到对应的图片的;
而在Framwork中 打包图片的方式,也有多种

一、使用Bundle

NSString *path = [[NSBundle mainBundle] pathForResource:@"Source" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:path];
return [UIImage imageNamed:@"test" inBundle:bundle compatibleWithTraitCollection:nil];

不论Framework是静态库或者动态库,都可以使用此种方式引入图片资源;

二、使用xcassets

我们可以在Framework内部,新建一个xcassets文件,把图片引入xcassets中;


xcassets文件,在编译成功后,会编译为Assets.car文件
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
return [UIImage imageNamed:@"test" inBundle:bundle compatibleWithTraitCollection:nil];

使用xcassets方式,Framework必须为动态库,静态库图片资源获取不到;

三、直接引入

NSBundle *bundle = [NSBundle bundleForClass:[self class]];
return [UIImage imageNamed:@"name" inBundle:bundle compatibleWithTraitCollection:nil];

此种方式,Framework必须为动态库,静态库图片资源获取不到;

上一篇 下一篇

猜你喜欢

热点阅读