NSBundle的创建和使用

1.创建步骤

  1)MacOS系统下,新建文件夹,修改文件名,放好资源;

  2)修改文件夹后缀为(.bundle);

  3)右键可查看包内容(也可以这时放入资源)。

2.使用

  1)获取自定义的Bundle

1     // 获取自定义的Bundle 两种方式Path和URL均可
2     // 常用Path
3     NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"Demo" ofType:@"bundle"]];
4     // URL
5     NSBundle *bundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"Demo" withExtension:@"bundle"]];

  2)获取自定义的Bundle中的资源

1     // 获取image
2     UIImage *image = [UIImage imageWithContentsOfFile:[bundle pathForResource:@"icon_logo" ofType:@"png"]];

3.快捷使用

1     // 常用
2     UIImage *image = [UIImage imageNamed:@"Demo.bundle/icon_logo.png"];

   [UIImage imageNamed:@"xxx.png"]作用范围是[NSBundle mainBundle],用于自定义的Bundle需要使用全路径,相比前面的方式还是比较简便快捷。

时常一个人发呆,看到宁静的天空。
原文地址:https://www.cnblogs.com/pinweyshg/p/5116878.html