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. RDD, DataFrame or Dataset

    总结: 1.RDD是一个Java对象的集合.RDD的优点是更面向对象,代码更容易理解.但在需要在集群中传输数据时需要为每个对象保留数据及结构信息,这会导致数据的冗余,同时这会导致大量的GC. 2.Da ...

  2. PAT 1138 Postorder Traversal [比较]

    1138 Postorder Traversal (25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  3. #C++初学记录

    输入与输出,头文件. #include<iostream> #include<algorithm> using namespace std; int main() { char ...

  4. 13信号signal

    信号 传送给进程的事件通知,完成异步通信 信号的产生 1.程序错误:硬件异常,除数为0,等 2.外部事件:定时器事件,按键中断(ctrl+c)等 3.显示请求:调用 kill,  raise 等信号发 ...

  5. [one day one question] 有没有免费接收短信验证用于注册的软件或者平台?

    问题描述: 想要批量注册撸羊毛,有手机短信验证码验证,这怎么破? 解决方案: 免费的肯定没有的,不过"一条短信收费一毛钱"倒是有一个,本人是亲自试用过,该平台收不到短信验证码不收费 ...

  6. 20145307陈俊达《网络对抗》shellcode注入&return to libc

    20145307陈俊达<网络对抗>shellcode注入 Shellcode注入 基础知识 Shellcode实际是一段代码,但却作为数据发送给受攻击服务器,将代码存储到对方的堆栈中,并将 ...

  7. 20145211黄志远《网络对抗》Exp9 Web安全基础实践

    20145211黄志远<网络对抗>Exp9 Web安全基础实践 基础问题回答 SQL注入攻击原理,如何防御? SQL注入攻击就是利用输入的机会构造自己期望的请求,比如破坏掉用户名或者密码验 ...

  8. Java多线程,线程交替执行

    两个线程,一个打印1-100的奇数,一个打印1-100的偶数:要求:线程1打印5个之后,线程2开始打印,线程2打印5个之后,线程1再开始打印,以此循环. Code: package com.qhong ...

  9. HttpContext.Current and Web Api

    Using HttpContext.Current in WebApi is dangerous because of async HttpContext.Current gets the curre ...

  10. shell 判断文件是否存在,没有则创建

    没有该文件则创建,有则 ls -l 输出文件信息. #!/bin/bash echo "enter the name:" read filename if test -e $fil ...