iOS_UIImage中 + imageNamed: 和 + imageWithContentsOfFile:两个方法的区别

本文主要是介绍iOS_UIImage中 + imageNamed: 和 + imageWithContentsOfFile:两个方法的区别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

说明:
UIImage中 + imageNamed: 和 + imageWithContentsOfFile: 都是我们常用的创建image对象方法, 但是它们之间还是有一些区别,特别是考虑线程安全, 系统内存优化方面

文章中尽量不使用或少使用封装, 目的是让大家清楚为了实现功能所需要的官方核心API是哪些(如果使用封装, 会在封装外面加以注释)

  • 此文章由 @Scott 编写. 经 @春雨,@黑子 审核. 若转载此文章,请注明出处和作者

+ imageNamed: 和 + imageWithContentsOfFile:两个方法的区别

核心API

Class : UIImage
Delegate : null
涉及的API:(API的官方详细注释(英文)详见本章结尾)

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

讨论:

+ imageNamed:
使用此方法创建image对象首先从系统缓存中查找, 如果缓存中没有, 再从磁盘或者资源目录中加载图片数据,生成image对象返回.同时, Apple官方提示此方法不确保线程的安全.
另外,使用此方法, 在iOS 4以后, 如果文件是PNG格式, 在参数部分指定文件名的时候,后面可以省略.PNG后缀.
另外Apple官方提示如果你只想显示图片,而不想添加进系统的缓存中时, 使用imageWithContentsOfFile:方法.

+ imageWithContentsOfFile:
相对imageNamed:方法, 此方法不会缓存图片对象. 在一定情况下, 会提高APP的内存使用效率.

Apple官方注释:

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 locates and loads the image data from disk or asset catelog, and then returns the resulting object. You can not assume that this method is thread safe.

On iOS 4 and later, if the file is in PNG format, it is not necessary to specify the .PNG filename extension. Prior to iOS 4, you must specify the filename extension.

If you have an image file that will only be displayed once and wish to ensure that it does not get added to the system’s cache, you should instead create your image using imageWithContentsOfFile:. This will keep your single-use image out of the system image cache, potentially improving the memory use characteristics of your app.

    • imageNamed 和 imageWithContentsOfFile两个方法的区别
      • 核心API
    • 讨论
    • API 官方注释英文


API 官方注释(英文)

/*** @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

这篇关于iOS_UIImage中 + imageNamed: 和 + imageWithContentsOfFile:两个方法的区别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/646896

相关文章

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Before和BeforeClass的区别及说明

《Before和BeforeClass的区别及说明》:本文主要介绍Before和BeforeClass的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Before和BeforeClass的区别一个简单的例子当运行这个测试类时总结Before和Befor

Maven 配置中的 <mirror>绕过 HTTP 阻断机制的方法

《Maven配置中的<mirror>绕过HTTP阻断机制的方法》:本文主要介绍Maven配置中的<mirror>绕过HTTP阻断机制的方法,本文给大家分享问题原因及解决方案,感兴趣的朋友一... 目录一、问题场景:升级 Maven 后构建失败二、解决方案:通过 <mirror> 配置覆盖默认行为1. 配置示

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

使用jenv工具管理多个JDK版本的方法步骤

《使用jenv工具管理多个JDK版本的方法步骤》jenv是一个开源的Java环境管理工具,旨在帮助开发者在同一台机器上轻松管理和切换多个Java版本,:本文主要介绍使用jenv工具管理多个JD... 目录一、jenv到底是干啥的?二、jenv的核心功能(一)管理多个Java版本(二)支持插件扩展(三)环境隔

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma

Mybatis Plus Join使用方法示例详解

《MybatisPlusJoin使用方法示例详解》:本文主要介绍MybatisPlusJoin使用方法示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录1、pom文件2、yaml配置文件3、分页插件4、示例代码:5、测试代码6、和PageHelper结合6

Java中实现线程的创建和启动的方法

《Java中实现线程的创建和启动的方法》在Java中,实现线程的创建和启动是两个不同但紧密相关的概念,理解为什么要启动线程(调用start()方法)而非直接调用run()方法,是掌握多线程编程的关键,... 目录1. 线程的生命周期2. start() vs run() 的本质区别3. 为什么必须通过 st

C#之List集合去重复对象的实现方法

《C#之List集合去重复对象的实现方法》:本文主要介绍C#之List集合去重复对象的实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C# List集合去重复对象方法1、测试数据2、测试数据3、知识点补充总结C# List集合去重复对象方法1、测试数据

SpringBoot读取ZooKeeper(ZK)属性的方法实现

《SpringBoot读取ZooKeeper(ZK)属性的方法实现》本文主要介绍了SpringBoot读取ZooKeeper(ZK)属性的方法实现,强调使用@ConfigurationProperti... 目录1. 在配置文件中定义 ZK 属性application.propertiesapplicati