IOS学习之NSBundle介绍和使用

bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-in). 对应bundle,

cocoa提供了类NSBundle.

我们现在用bundle获取程序里的一张图片,并显示到View上。

新建一个Single View Application,并在加入viewDidLoad方法里加入如下代码:



//    通过使用下面的方法得到程序的main bundlewww.it165.net
    NSBundle *mainBundle = [NSBundle mainBundle];
    
    NSString *imagePath = [mainBundle pathForResource:@"QQ20120616-1" ofType:@"png"];
    NSLog(@"%@", imagePath);
    UIImage *image = [[UIImage alloc]initWithContentsOfFile:imagePath];
    UIImageView  *imageView = [[UIImageView alloc] initWithImage:image]; 
    [self.view addSubview:imageView];

你可能感兴趣的:(IOS学习之NSBundle介绍和使用)