IT 在线教育平台———麦子学院: IOS 开发中, bundle 是一个目录, 其中包含了程序会使用到的资源. 这些资源包含了如图像, 声音, 编译好的代码,nib 文件( 用户也会把 bundle 称为 plug-in). 对应 bundle,cocoa 提供了类 NSBundle. 我们现在用 bundle 获取程序里的一张图片,并显示到 View 上。新建一个 Single View Application ,并在加入 viewDidLoad 方法里加入如下代码: 1. // 通过使用下面的方法得到程序的 main bundle 2. NSBundle *mainBundle = [NSBundle mainBundle]; 3. 4. NSString *imagePath = [mainBundle pathForResource:@"QQ20120616-1" ofType:@"png"]; 5. NSLog(@"%@", imagePath); 6. UIImage *image = [[UIImage alloc]initWithContentsOfFile:imagePath]; 7. UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 8. [ addSubview:imageView]; 在项目上右键, add 图片文件图片文件 。运行程序: 打印出来图片路径如下/Users/rongfzh/Library/Application Support/iPhone Simulator/-5EEE-4C2F-B0CB- 4C3F02B996D2/- 我们可以看到,图片在 这个包里, 图片显示: IT 在线教育平台———麦子学院: 出来并显示了。如需了解更多 IOS 开发知识,请至麦子学院官网查看。
IOS开发的教程NSBundle简介 来自淘豆网m.daumloan.com转载请标明出处.