imagenamed和imageWithContentOfFile的区别
@implementation ViewController /**
图片的两种加载方式:
1> imageNamed:
a. 就算指向它的指针被销毁,该资源也不会被从内存中干掉
b. 放到Assets.xcassets的图片,默认就有缓存
c. 图片经常被使用 2> imageWithContentsOfFile:
a. 指向它的指针被销毁,该资源会被从内存中干掉
b. 放到项目中的图片就不由缓存
c. 不经常用,大批量的图片 */ // 初始化一些数据
- (void)viewDidLoad {
[super viewDidLoad]; // 1.加载所有的站立图片
self.standImages = [self loadAllImagesWithimagePrefix:@"stand" count:10]; // 2.加载所有的小招图片
self.smallImages = [self loadAllImagesWithimagePrefix:@"xiaozhao3" count:39]; // 3.加载所有的大招图片
self.bigImages = [self loadAllImagesWithimagePrefix:@"dazhao" count:87]; // 4.站立
[self stand];
} /**
* 加载所有的图片
*
* @param imagePrefix 名称前缀
* @param count 图片的总个数
*/
- (NSArray *)loadAllImagesWithimagePrefix:(NSString *)imagePrefix count:(int)count{
NSMutableArray<UIImage *> *images = [NSMutableArray array];
for (int i=0; i<count; i++) {
// 获取所有图片的名称
NSString *imageName = [NSString stringWithFormat:@"%@_%d",imagePrefix, i+1];
// 创建UIImage
// UIImage *image = [UIImage imageNamed:imageName];
NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
// 装入数组
[images addObject:image];
}
return images;
} /**
* 站立
*/
- (IBAction)stand {
/*
// 2.设置动画图片
self.imageView.animationImages = self.standImages; // 3.设置播放次数
self.imageView.animationRepeatCount = 0; // 4.设置播放的时长
self.imageView.animationDuration = 0.6; // 5.播放
[self.imageView startAnimating];
*/
[self palyZhaoWithImages:self.standImages count:0 duration:0.6 isStand:YES];
} /**
* 小招
*/
- (IBAction)smallZhao {
/*
// 2.设置动画图片
self.imageView.animationImages = self.smallImages; // 3.设置动画次数
self.imageView.animationRepeatCount = 1; // 4.设置播放时长
self.imageView.animationDuration = 1.5; // 5.播放
[self.imageView startAnimating]; // 6.站立(延迟)
[self performSelector:@selector(stand) withObject:nil afterDelay:self.imageView.animationDuration];
*/
[self palyZhaoWithImages:self.smallImages count:1 duration:1.5 isStand:NO];
} /**
* 大招
*/
- (IBAction)bigZhao {
/*
// 2.设置动画图片
self.imageView.animationImages = self.bigImages; // 3.设置动画次数
self.imageView.animationRepeatCount = 1; // 4.设置播放时长
self.imageView.animationDuration = 2.5; // 5.播放
[self.imageView startAnimating]; // 6.站立
[self performSelector:@selector(stand) withObject:nil afterDelay:self.imageView.animationDuration];
*/
[self palyZhaoWithImages:self.bigImages count:1 duration:2.5 isStand:NO];
} /**
* 游戏结束
*/
- (IBAction)gameOver {
self.standImages = nil;
self.smallImages = nil;
self.bigImages = nil; self.imageView.animationImages = nil;
} /**
* 放招
*
* @param images 图片数组
* @param count 播放次数
* @param duration 播放时间
* @param isStand 是否站立
*/
- (void)palyZhaoWithImages:(NSArray *)images count: (NSInteger)count duration:(NSTimeInterval)duration isStand:(BOOL)isStand{
// 1.设置动画图片
self.imageView.animationImages = images; // 2.设置动画次数
self.imageView.animationRepeatCount = count; // 3.设置播放时长
self.imageView.animationDuration = duration; // 4.播放
[self.imageView startAnimating]; // 5.站立
if (!isStand) {
[self performSelector:@selector(stand) withObject:nil afterDelay:self.imageView.animationDuration];
}
} @end
imagenamed和imageWithContentOfFile的区别的更多相关文章
- imageNamed和dataWithContentsOfFile的区别(1)
imageNamed和dataWithContentsOfFile的区别 imagecacheuiviewextensionprocessingxcode 最近老是受iphone内存问题的困扰,找了些 ...
- UIImage imageNamed和UIImage imageWithContentsOfFile区别
UIImage imageNamed和 [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageNam ...
- imageNamed 与 imageWithContentsOfFile的区别
如题,是不是大家为了方便都这样加载图片啊 myImage = [UIImage imageNamed:@"icon.png"];那么小心了这种方法在一些图片很少,或者图片很小的程序 ...
- iOS imageNamed 与 imageWithContentsOfFile 的区别
imageNamed 方法 1)后面的参数是icon的名字,图片可以存在项目中,也可以存在Asset中 2)该方法只适合一些中小型的图片读取,而一些比较大的资源图片并不适合用这个方法 3)这个方法加载 ...
- imageNamed和imageWithContextOfFile的区别?哪个性能高
imageNamed性能高 1.用imageNamed的方式加载时,图片使用完毕后缓存到内存中,内存消耗多,加载速度快.即使生成的对象被 autoReleasePool释放了,这份缓存也不释放,如果图 ...
- A Bit Of Knowledge
iOS推崇使用png格式的图片,说这样不会失帧 imageNamed 和 imageWithContentOfFile的区别 imageNamed会使用系统缓存,对重复加载的图片速度会快一些,效果好. ...
- iOS内置图片瘦身思路整理
一.前言 前段时间注意到我们APP的包大小超过100MB了,所以随口跟老板说了下能否采用字体文件(.ttf)替代PNG图片,老板对应用瘦身很感兴趣因此让我做下技术调研.这篇文章主要是将我们的各个技术方 ...
- iOS 内置图片瘦身
一.iOS 内置资源的集中方式 1.1 将图片存放在 bundle 这是一种很常见的方式,项目中各类文件分类放在各个 bundle 下,项目既整洁又能达到隔离资源的目的.采用 bundle 的加载方式 ...
- iOS开发——UI基础-UIImage,UIImageView的使用
1.UIImage 创建UIImage的两种方法 UIImage *image = [UIImage imageNamed:imageNmae]; UIImage *image = [UIImage ...
随机推荐
- 使用 Eclipse 创建一个静态的登录页面
要求: 使用 Eclipse 创建一个静态的登录页面 实现步骤: 在 Eclipse 中,点击"File",显示菜单,选择"New" "Other&q ...
- Pycharm的界面修改与基本设置
Pycharm的一些基本设置 设置字体大小与字体样式 首先我们打开Pycharm,点击左上角File,找到sitting 点击后就能进入设置界面 xdm,我将介绍两种更改字体的设置 方法一 设置界面找 ...
- 基于MCRA-OMLSA的语音降噪(三):实现(续)
上篇文章(基于MCRA-OMLSA的语音降噪(二):实现)讲了基于MCRA-OMLSA的语音降噪的软件实现.本篇继续讲,主要讲C语言下怎么对数学库里的求平方根(sqrt()).求自然指数(exp()) ...
- python日志按天分割,保存近一个月日志,日志自动清理
python日志按天分割,保存近一个月日志 import os import logging import re from logging.handlers import TimedRotatingF ...
- ajax 异步 提交 含文件的表单
1.前言 需求是使用 jquery 的 ajax 异步提交表单,当然,不是简单的数据,而是包含文件数据的表单.于是我想到了 new FormData() 的用法, 可是仍然提交失败,原来是ajax的属 ...
- 创建react开发环境
准备工作 1.下载node.js(http://nodejs.cn/download/)推荐下载长期支持的版本 2.下载cnpm(https://jingyan.baidu.com/article/9 ...
- Spark案例练习-PV的统计
关注公众号:分享电脑学习回复"百度云盘" 可以免费获取所有学习文档的代码(不定期更新) 云盘目录说明: tools目录是安装包res 目录是每一个课件对应的代码和资源等doc ...
- openlayers素材网站
1.教程网站 http://weilin.me/ol3-primer/ch05/05-03.html 2.特效气象图 https://earth.nullschool.net/zh-cn/#curre ...
- Git 的配置 config
Git 的配置 config Git 的配置 config config 文件简述 config 文件位置 信息查询 修改 config 文件 编辑配置文件 增加指定配置项 删除指定配置项 自助餐 ...
- 《剑指offer》面试题19. 正则表达式匹配
问题描述 请实现一个函数用来匹配包含'. '和'*'的正则表达式.模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(含0次).在本题中,匹配是指字符串的所有字符匹配整个模式. ...