iOS中bundle的意义
什么是bundle?
bundle就是一个文件夹,按照一定标准组织的目录结构。每个iOS APP至少有一个main bundle,这个main bundle包含了app的二进制代码及任何你用到的资源,如图片,声音,HTML文件等。换句话说,主bundle包含了所有资源,这些资源会被编译成二进制代码提交到App Store上。
bundle与普通的文件夹有什么区别?
1.cocoa touch框架提供了一个接口,可以很方便的访问bundle及其内部资源。
2.如果将bundle加入了Xcode中,则在本地目录下任意更改bundle中的内容,Xcode中的bundle都会察觉到,并且将变化内容同步进来。如果将普通文件夹加入Xcode,在本地目录下删除该目录下的资源,被删除的资源在Xcode中会变成红色,需要手动再处理一遍。总结,bundle中的任何资源变动,Xcode都能同步过去,但普通文件夹却不行。
使用bundle有什么意义?
在我们的APP国际化的时候,bundle就上场了。如果不使用bundle,需要程序员自己维护不同国家和地区使用的资源,有些是公用的,而有些是本地化的,维护成本过高。有了bundle,我们可以按照bundle的标准去存放资源文件,无需写代码判断本地语言。方法很简单,创建对应的“本地化文件夹”,比如需求是不同区域的“test.png”显示的内容不同,我们可以创建两个本地化文件夹zh.lproj和en.lproj,分别把两幅同名但内容不同的test.png放入对应的文件夹中即可。
如何使用bundle?
读取bundle中的image对象:
NSString*alanSugarFilePath = [[NSBundle mainBundle]pathForResource:@"AlanSugar" ofType:@"png"];
if([alanSugarFilePath length]>0){
UIImage*image=[UIImage imageWithContentsOfFile:alanSugarFilePath];
if(image!=nil){
NSLog(@"Successfully loaded the file as an image.");
}else{
NSLog(@"Failed to load the file as an image.");
}
}else{
NSLog(@"Could not find this file in the main bundle.");
}
读取bundle中的NSData对象:
if([alanSugarFilePath length]>0){
NSError *readError=nil;
NSData *dataForFile= [[NSData alloc] initWithContentsOfFile:alanSugarFilePath
options:NSMappedRead
error:&readError];
if(readError==nil&&dataForFile!=nil){
NSLog(@"Successfully loaded the data.");
}else if(readError==nil&& dataForFile==nil){
NSLog(@"No data could be loaded.");
}else{
NSLog(@"An error occured while loading data. Error = %@",readError);
}
} else{
NSLog(@"Could not find this file in the main bundle.");
}
读取子bundle中的NSData对象,如Resources(mainBundle)->Images(childBundle)->AlanSugar.png(file)
NSString*resourcesBundlePath = [[NSBundle mainBundle]pathForResource:@"Resources" ofType:@"bundle"];
if([resourcesBundlePath length]>0){
NSBundle*resourcesBundle=[NSBundle bundleWithPath: resourcesBundlePath];
if(resourcesBundle!=nil){
NSString*pathToAlanSugarImage=[resourcesBundle pathForResource: @"AlanSugar"
ofType:@"png"
inDirectory:@"Images"];
if([pathToAlanSugarImage length]>0){
UIImage*image=[UIImage imageWithContentsOfFile: pathToAlanSugarImage];
if(image!=nil){
NSLog(@"Successfully loaded the image from the bundle.");
}else{
NSLog(@"Failed to load the image.");
}
}else{
NSLog(@"Failed to find the file inside the bundle.");
}
}else{
NSLog(@"Failed to load the bundle.");
}
} else{
NSLog(@"Could not find the bundle.");
}
可用[pathsForResourcesOfType:inDirectory: ]方法找到指定文件夹下的所有资源:
NSString*resourcesBundlePath= [[NSBundle mainBundle]pathForResource:@"Resources" ofType:@"bundle"];
if([resourcesBundlePath length]>0){
NSBundle*resourcesBundle=[NSBundle bundleWithPath: resourcesBundlePath];
if(resourcesBundle!=nil){
NSArray*PNGPaths=[resourcesBundle pathsForResourcesOfType:@"png"inDirectory:@"images"];
[PNGPaths enumerateObjectsUsingBlock:^(idobj,NSUInteger idx,BOOL*stop) {
NSLog(@"Path %lu = %@", (unsigned long)idx+1,obj);}];
}else{
NSLog(@"Failed to load the bundle.");
}
} else{
NSLog(@"Could not find the bundle.");
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
2014-08-04 城市三维地下管线管理系统
2012-08-04 修复VS 2010的Help Library管理器
2012-08-04 Vs2010中文版MSDN 安装方法
2012-08-04 编译的时候生成.g.cs还有.g.i.cs,有什么区别?
2010-08-04 OraOps10.dll不能加载