zoukankan      html  css  js  c++  java
  • imageNamed和imageWithContentsOfFile的区别

    iOS中从程序bundle中加载UIImage一般有两种方法。第一种比较常见:imageNamed。第二种方法很少使

    用:imageWithContentsOfFile
    为什么有两种方法完成同样的事情呢? imageNamed的优点在于可以缓存已经加载的图片。苹果的文档中有如下说法:

    This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object. 这种方法会在系统缓存中根据指定的名字寻找图 片,如果找到了就返回。如果没有在缓存中找到图片,该方法会从指定的文件中加载图片数据,并将其缓存起来, 然后再把结果返回。

    而imageWithContentsOfFile方法只是简单的加载图片,并不会将图片缓存起来。 这两个方法的使用方法如下:

    1. UIImage *img = [UIImage imageNamed:@"myImage"]; // caching
    2. // or
    3. UIImage *img = [UIImage imageWithContentsOfFile:@"myImage"]; // no caching

    那么该如何选择呢?

    如果加载一张很大的图片,并且只使用一次,那么就不需要缓存这个图片。这种情 况imageWithContentsOfFile比较合适——系统不会浪费内存来缓存图片。

    然而,如果在程序中经常需要重用的图片,那么最好是选择imageNamed方法。这种方法可以节省出每次都从磁 盘加载图片的时间。 

  • 相关阅读:
    11.29 两百字
    Django:基础知识
    deepdiff:对比文件
    openpyxl:二次封装
    openpyxl:openpyxl的随笔
    web:pykeyboard、pymouse得使用
    Jmeter:如何让线程顺序执行
    Jmeter:如何将上一个线程得变量使用到下一个线程中,将上一个线程变量设置为全局变量
    Jmeter:BeanShell中的vars.put(string,string)
    Jmeter:数据库查询当天的日期
  • 原文地址:https://www.cnblogs.com/woaixixi/p/4936371.html
Copyright © 2011-2022 走看看