xcode资源管理

1. 在根目录放图片。

    UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ok.png"]];//也可以直接填"ok",网上是说,如果扩展名是png就可以省略。
    image.frame = CGRectMake(0, 0, 320, 320);
    [self.view addSubview:image];

2. 在实目录下放图片。

    NSString *themePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"img/ok.png"];
    NSLog(@"%@", themePath);
    UIImage *image = [UIImage imageWithContentsOfFile:themePath];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [self.view addSubview:imageView];

这个themePath打印出来是

/var/containers/Bundle/Application/63DC9D8A-2249-4BC0-B220-4EF18C244784/ImageTest.app/img/ok.png

3. 在bundle里面加载资源。bundle是一个很神奇的技术,可以把资源打包。

    NSString *path = [NSString stringWithFormat:@"QPSDK.bundle/root_bg.png"];  //QPSDK.bundle是bundle的文件名。
    UIImage *image = [UIImage imageNamed:path];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [self.view addSubview:imageView];

4. 读取assets里面的图片。其实这个和第一点提到的代码是一样的。只是这里的123是像集的名字。

    UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"123"]];  //123是像集的名字,没有扩展名。
    image.frame = CGRectMake(0, 0, 320, 320);
    [self.view addSubview:image];

 

posted on 2017-08-20 14:17  angelshelter  阅读(177)  评论(0编辑  收藏  举报

导航