iOS加载图片imageName,imageWithContentsOfFile

iOS加载图片imageName,imageWithContentsOfFile,第1张

1、使用imageName:加载图片

2、使用imageWithContentsofFile:加载图片

iOS7与iOS8的适配

1、 [UIImage imageWithContentsOfFile:] 路径需要加上沙盒路径以及你的文件名

例如我的:

2、iOS7:当从子bundle中读取图片时,文件名要加上 @2xpng

UIImage加载图片有两种方式:imageNamed 和 imageWithContentsOfFile

UIImage *image = [UIImage imageNamed:@“xxx”]

NSString *filePath = [[NSBundle mainBundle] pathForResource:@“xxx” ofType:@“xxx”]

UIImage *image = [UIImage imageWithContentsOfFile:filePath]

imageNamed 方法会自动缓存新加载的图片并重复利用,推荐用于尺寸较小并且反复使用的图片资源。imageNamed先根据指定的图片资源名在系统缓存中搜索图片资源,找到返回,找不到就到硬盘中重新加载图片并缓存。

imageWithContentsOfFile 方法直接根据路径加载图片,没有缓存和取缓存的过程,将图片转化成数据对象进行加载,推荐用于尺寸很大但不常用的图片。imageWithContentsOfFile不可以直接加载Assets里的图片,需要将图片拖入工程目录。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11467151.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存