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 ...
随机推荐
- html简单随机抽奖页面(在线抽奖、随机选取、自动挑选)
下载: https://download.csdn.net/download/weixin_44893902/20366745 效果: 代码: <!doctype html> <ht ...
- centos6.5-nginx搭建
一.安装nginx 1.安装相关组件 yum -y install pcre-devel zlib-devel 2.创建启动用户 useradd -M -s /sbin/nologin nginx t ...
- 论文翻译:2021_Semi-Blind Source Separation for Nonlinear Acoustic Echo Cancellation
论文地址:https://ieeexplore.ieee.org/abstract/document/9357975/ 基于半盲源分离的非线性回声消除 摘要: 当使用非线性自适应滤波器时,数值模型与实 ...
- java POJO中 Integer 和 int 的不同,用int还是用Integer
https://www.jianshu.com/p/ff535284916f [int和Integer的区别] int是java提供的8种原始类型之一,java为每个原始类型提供了封装类,Intege ...
- spring security +MySQL + BCryptPasswordEncoder 单向加密验证 + 权限拦截 --- 心得
1.前言 前面学习了 security的登录与登出 , 但是用户信息 是 application 配置 或内存直接注入进去的 ,不具有实用性,实际上的使用还需要权限管理,有些 访问接口需要某些权限才可 ...
- Thrift框架-安装
1.前言 今天接触了使用 PRC[远程过程调用协议]的Thrift 框架 ,留下随笔心得,这是安装篇 2.下载 去apache官网下载Thrift脚本编译程序,window则下载一个exe文件,然后 ...
- debian8.4系统安装后的一些设置
1.添加软件源 su到root用户vi /etc/apt/sources.list 也可用gedit /etc/apt/sources.list (gnome下用,如果kde下则用 ...
- 函数实现将 DataFrame 数据直接划分为测试集训练集
虽然 Scikit-Learn 有可以划分数据集的函数 train_test_split ,但在有些特殊情况我们只希望它将 DataFrame 数据直接划分为 train, test 而不是像 tr ...
- 使用.NET 6开发TodoList应用(30)——实现Docker打包和部署
系列导航及源代码 使用.NET 6开发TodoList应用文章索引 需求 .NET 6 Web API应用使用最多的场景是作为后端微服务应用,在实际的项目中,我们一般都是通过将应用程序打包成docke ...
- C\C++ IDE 比较以及调试
C\C++ IDE 比较以及调试 内容概要 这个作业属于哪个课程 2022面向对象程序设计 这个作业要求在哪里 2022面向对象程序设计寒假作业1 这个作业的目标 IDE 选择以及代码调试 作业正文 ...