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

 

相关文章:

  • 2021-12-12
  • 2022-01-18
  • 2021-04-13
  • 2021-05-27
  • 2021-09-27
  • 2021-04-15
猜你喜欢
  • 2021-07-07
  • 2022-12-23
  • 2021-07-25
  • 2021-05-17
相关资源
相似解决方案