iOS-生成Bundle包-引入bundle-使用bundle

本文介绍了iOS开发中的Bundle文件概念,包括其定义、特点及如何在项目中创建和使用Bundle文件来管理资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在我们使用第三方框架时,常常看到XXX.bundle的文件。

我们找到该文件,显示包内容,大致看到很多资源文件:图片、配置文本、XIB文件……


什么是Bundle文件?

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


Bundle文件的特点?

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


制作Bundle


1.新建bundle项目





VC获得bundle中的资源


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

NSBundle *resourceBundle = [NSBundle bundleWithPath:bundlePath];

UIViewController *vc = [[UIViewController alloc] initWithNibName:@"vc_name" bundle:resourceBundle];


图片获得bundle中的资源


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

UIImage *image = [UIImage imageNamed:@"MyBundle.bundle/img_collect_success"];

[imgView setImage:image];


或者


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

NSString *imgPath= [bundlePath stringByAppendingPathComponent :@"img_collect_success.png"];

UIImage *image_1=[UIImage imageWithContentsOfFile:imgPath];

[imgView setImage:image_1];


当然,可以写成预编译语句:

#define MYBUNDLE_NAME @ "MyBundle.bundle"

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

#define MYBUNDLE [NSBundle bundleWithPath: MYBUNDLE_PATH]


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值