iOS_UIImage中 + imageNamed: 和 + imageWithContentsOfFile 二者之间的区别

这2种方式是我们长使用的加载一张图片的方法,但是他们之间有什么去区别呢?

官方文档:

/**
 * @brief   Returns the image object associated with the specified filename.
 *
 * @param   <name>  The name of the file. If this is the first time the image is being loaded, the method looks for an image with the specified name in the application’s main bundle. 
 *
 * @return  The image object for the specified file, or nil if the method could not find the specified image.
 */

+ (UIImage *)imageNamed:(NSString *)name
/**
 * @brief   Creates and returns an image object by loading the image data from the file at the specified path.
 *
 * @param <path>    The full or partial path to the file
 *
 * @return A new image object for the specified file, or nil if the method could not initialize the image from the specified file.
 */

+ (UIImage *)imageWithContentsOfFile:(NSString *)path

 说下区别吧:

+ (UIImage *)imageNamed:(NSString *)name 加载图片,首先从系统缓存中查找, 如果缓存中没有, 再从磁盘或者资源目录中加载图片数据,生成image对象返回.同时, Apple官方提示此方法不确保线程的安全.

+ (UIImage *)imageWithContentsOfFile:(NSString *)path 加载图片:此方法不会缓存图片对象. 在一定情况下, 会提高APP的内存使用效率.


原文地址:https://www.cnblogs.com/LzwBlog/p/5752142.html