图片加载方式

作者: 生如夏花逝如秋叶 | 来源:发表于2016-08-09 14:56 被阅读98次
    加载图片的方式:
    • 方法一:imageNamed:

          UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
      
          imageView.image = [UIImage imageNamed:@"1"];
      
      
    • 方法二:imageWithContentsOfFile:

          UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
      
          NSString *path = [[NSBundle mainBundle] pathForResource:@"0" ofType:@"png"];
      
          imageView.image = [UIImage imageWithContentsOfFile:path];
      
      
    图片资源存取:
    • 1.加载Assets.xcassets这里面的图片:

      • 1> 打包后变成Assets.car
      • 2> 拿不到图片路径
      • 3> 只能通过imageNamed:来加载图片
      • 4> 不能通过imageWithContentsOfFile:来加载图片
    • 2.放到项目中的图片:

      • 1> 可以拿到图片路径
      • 2> 能通过imageNamed:来加载图片
      • 3> 能通过imageWithContentsOfFile:来加载图片
    图片两种加载方式的内存缓存:
    • 1.通过imageNamed:加载图片
      • a. 就算指向它的指针被销毁,该资源也不会被从内存中干掉
      • b. 放到Assets.xcassets的图片,默认就有缓存
      • c. 使用场景:图片经常被使用
    • 2.通过imageWithContentsOfFile:加载图片
      • a. 指向它的指针被销毁,该资源会被从内存中干掉
      • b. 放到项目中的图片没有缓存
      • c. 使用场景:不经常使用的图片,大批量的图片

    相关文章

      网友评论

        本文标题:图片加载方式

        本文链接:https://www.haomeiwen.com/subject/rpldsttx.html