1、[NSBundle mainBundle],文件夹其实是Group,如左侧的树形文件管理器

Build之后,文件直接就复制到了根目录下,于是读取的方法,应该是这样:

NSString *earth = [[NSBundle mainBundle] pathForResource:@"Brad Cox" ofType:@"png"];

2、使用文件夹的时候,Build结果

依然使用同样的方法,不需要制定文件夹路径

//inDirectory参数可有可无
NSString *earth = [[NSBundle mainBundle] pathForResource:@"Matthijs" ofType:@"png" inDirectory:@"Images/MoreImages"];
NSString *earth = [[NSBundle mainBundle] pathForResource:@"Matthijs" ofType:@"png"];

编译之后,mainBundle的资源都是放到RootFolder下,所以,可以直接访问,不要指定内部路径

3、使用其他的Bundle

NSString *resourceBundle = [[NSBundle mainBundle] pathForResource:@"Resources" ofType:@"bundle"];
NSLog(@"resourceBundle: %@", resourceBundle);
NSString *earth = [[NSBundlebundleWithPath:resourceBundle] pathForResource:@"Matthijs"ofType:@"jpg"inDirectory:@"Images/MoreImages"];
NSLog(@"path: %@", earth);

  

使用Custom bundle,访问内部的子文件夹,需要指定inDirectory参数,这个mainBundle不同,这是为何?

  

注意:Since
bundles other than the main bundle can have folders embedded inside them, to access
files inside folders of a bundle other than the main bundle it is best to use the pathFor
Resource:ofType:inDirectory: method of NSBundle to explicitly specify the folder in
which a specific file/resource exists.

  原文地址:http://www.cnblogs.com/iihe602/archive/2013/01/17/2865280.html

IOS NSBundle的使用,注意mainBundle和Custom Bundle的区别的更多相关文章

  1. NSBundle的使用,注意mainBundle和Custom Bundle的区别

    1.[NSBundle mainBundle],文件夹其实是Group,如左侧的树形文件管理器 Build之后,文件直接就复制到了根目录下,于是读取的方法,应该是这样: NSString *earth ...

  2. 【转】NSBundle的使用,注意mainBundle和Custom Bundle的区别

    1.[NSBundle mainBundle],文件夹其实是Group,如左侧的树形文件管理器 Build之后,文件直接就复制到了根目录下,于是读取的方法,应该是这样: NSString *earth ...

  3. IOS NSBundle 的理解和 mainBundle 类方法详解

    常看到类似的 NSString *file = [[NSBundle mainBundle] pathForResource:name ofType:nil]; 这样的代码,用来获取 file 的完全 ...

  4. iOS 关于nil和Nil及null与<null>的区别

    问题是这样的. NSDictionary *sample = [NSJSONSerialization JSONObjectWithData:received options:NSJSONReadin ...

  5. iOS 杂笔-20(UIView和CALayer的区别与联系)

    iOS 杂笔-20(UIView和CALayer的区别与联系) 每个 UIView 内部都有一个 CALayer 在背后提供内容的绘制和显示,并且 UIView 的尺寸样式都由内部的 Layer 所提 ...

  6. iOS [[NSBundle mainBundle] pathForResource:@"" ofType:@""]无法获取到文件

    将一个文件导入到工程中后,用[[NSBundle mainBundle] pathForResource:@"" ofType:@""]来获取到该文件时,一直无 ...

  7. iOS:NSBundle的具体介绍

    NSBundle介绍:它是一个单例类,用来加载资源 bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-i ...

  8. NSBundle的理解和mainBundle的基本介绍

    一.NSBundle NSBundle是cocoa为bundle提供的一个类,bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像.声音.编译好的代码.nib文件.(用户也会把 ...

  9. IOS NSBundle使用(访问文件夹)

    NSBundle的相关信息 1.一个NSBundle代表一个文件夹,利用NSBundle能访问对应的文件夹 2.利用mainBundle就可以访问软件资源包中的任何资源 3.模拟器应用程序的安装路径: ...

随机推荐

  1. Linux系统——搭建FTP方式的本地定制化Yum仓库

    (1)搭建公网源yum仓库 安装wget aliyun源 # wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epe ...

  2. 【开源】检查更新程序 CheckUpdate.Net 的实现

    访问最新源代码及更新历史:http://git.oschina.net/xcong/CheckUpdate.Net DownLoad 更新历史 version 1.2 [新增]添加UpdateFile ...

  3. redis未授权访问漏洞总结

    Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. 漏洞介绍: Redis 默认情况下,会绑定在 0.0.0.0 ...

  4. 20145204《Java程序设计》第10周学习总结

    网络编程 网络编程:在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置,或者接收到指定的数据,这个就是狭义的网络编程范畴.在发送和接收数据时,大部分的程序设计 ...

  5. ubuntu 18.04 64bit build tensorflow report error:C++ compilation of rule '//tensorflow/core/kernels:broadcast_to_op' failed (Exit 4)

    注意:笔者未能在Ubuntu18.04 64bit下成功从源码编译cpu版本的tensorflow Ans: 1.You are likely running out of memory. Try r ...

  6. LS1021ATWR开发板启动日志分析

    一.背景 LS1021ATWR开发板运行官方的openwrt系统 二.日志分析 2.1 linux相关日志 root@OpenWrt:/# reboot  重启 root@OpenWrt:/# [ 2 ...

  7. js的函数作用域

    1.js作用域 //在函数内部声明的变量,如果不加var,则自动变成window的成员//预处理:扫描代码,看到var或者函数就生效./*function f(){a = 8;//var a = 8; ...

  8. (译)综合指南:通过Ubuntu 16.04上从Source构建来安装支持GPU的Caffe2

    (译)综合指南:通过Ubuntu 16.04上从Source构建来安装支持GPU的Caffe2 译者注: 原文来自:https://tech.amikelive.com/node-706/compre ...

  9. 使用ntpdate工具校正linux服务器时间

    当Linux服务器的时间不对的时候,可以使用ntpdate工具来校正时间. 安装:yum install ntpdate ntpdate简单用法: # ntpdate ip # ntpdate 210 ...

  10. 学习maven,看思维导图就够了

    一直在用 Mindjet MindManager和xmind 做思维导图.以前在用思维导图写读书笔记时,总是想要事无巨细地写清楚每个知识点,现在想来是事倍功半.思维导图应该是一个帮助我们梳理知识点.理 ...