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];

 

转载于:https://www.cnblogs.com/angelshelter/p/7399854.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值