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

相关文章

Springboot控制反转与Bean对象的方法

《Springboot控制反转与Bean对象的方法》文章介绍了SpringBoot中的控制反转(IoC)概念,描述了IoC容器如何管理Bean的生命周期和依赖关系,它详细讲解了Bean的注册过程,包括... 目录1 控制反转1.1 什么是控制反转1.2 SpringBoot中的控制反转2 Ioc容器对Bea

在Dockerfile中copy和add的区别及说明

《在Dockerfile中copy和add的区别及说明》COPY和ADD都是Dockerfile中用于文件复制的命令,但COPY仅用于本地文件或目录的复制,不支持自动解压缩;而ADD除了复制本地文件或... 目录在dockerfile中,copy 和 add有什么区别?COPY 命令ADD 命令总结在Doc

C++实现回文串判断的两种高效方法

《C++实现回文串判断的两种高效方法》文章介绍了两种判断回文串的方法:解法一通过创建新字符串来处理,解法二在原字符串上直接筛选判断,两种方法都使用了双指针法,文中通过代码示例讲解的非常详细,需要的朋友... 目录一、问题描述示例二、解法一:将字母数字连接到新的 string思路代码实现代码解释复杂度分析三、

mysql8.0无备份通过idb文件恢复数据的方法、idb文件修复和tablespace id不一致处理

《mysql8.0无备份通过idb文件恢复数据的方法、idb文件修复和tablespaceid不一致处理》文章描述了公司服务器断电后数据库故障的过程,作者通过查看错误日志、重新初始化数据目录、恢复备... 周末突然接到一位一年多没联系的妹妹打来电话,“刘哥,快来救救我”,我脑海瞬间冒出妙瓦底,电信火苲马扁.

SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)

《SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)》本文介绍了如何在SpringBoot项目中使用Jasypt对application.yml文件中的敏感信息(如数... 目录SpringBoot使用Jasypt对YML文件配置内容进行加密(例:数据库密码加密)前言一、J

Spring Boot 中正确地在异步线程中使用 HttpServletRequest的方法

《SpringBoot中正确地在异步线程中使用HttpServletRequest的方法》文章讨论了在SpringBoot中如何在异步线程中正确使用HttpServletRequest的问题,... 目录前言一、问题的来源:为什么异步线程中无法访问 HttpServletRequest?1. 请求上下文与线

解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题

《解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题》在Spring开发中,@Autowired注解常用于实现依赖注入,它可以应用于类的属性、构造器或setter方法上,然... 目录1. 为什么 @Autowired 在属性上被警告?1.1 隐式依赖注入1.2 IDE 的警告:

SpringBoot快速接入OpenAI大模型的方法(JDK8)

《SpringBoot快速接入OpenAI大模型的方法(JDK8)》本文介绍了如何使用AI4J快速接入OpenAI大模型,并展示了如何实现流式与非流式的输出,以及对函数调用的使用,AI4J支持JDK8... 目录使用AI4J快速接入OpenAI大模型介绍AI4J-github快速使用创建SpringBoot

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

python 3.8 的anaconda下载方法

《python3.8的anaconda下载方法》本文详细介绍了如何下载和安装带有Python3.8的Anaconda发行版,包括Anaconda简介、下载步骤、安装指南以及验证安装结果,此外,还介... 目录python3.8 版本的 Anaconda 下载与安装指南一、Anaconda 简介二、下载 An