ios 图片的两种加载方式
控件加载图片,plist,懒加载,序列帧动画,添加动画效果。
IOS中有2种加载图片的方式、
方式一:有缓存(图片所占用的内存会一直停留在程序中)
- + (UIImage *)imageNamed:(NSString *)name;
注:必须是png文件。需要把图片添加到 images.xcassets中
例如:
- @property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
- self.iconImageView.image=[UIImage imageNamed:@"icon"];
方式二:无缓存(图片所占用的内存会在一些特定操作后被清除)
.jpg格式的图片只能用无缓存方式加载
- + (UIImage *)imageWithContentsOfFile:(NSString *)path
- - (id)initWithContentsOfFile:(NSString *)path;
path是图片的全路径
其中又有:分组方式使用图片和不分组方式使用图片
分组方式导入的图片是蓝色的文件夹,创建UIImage时,需要完整的路径名(mainBundle +图片名字)
不分组方式导入的图片是黄色的文件夹,创建UIImage时,不需要完整的路径名(mianBundle+路径名+图片名字)
例如:
- /* 不分组方式来使用图片 文件夹颜色为黄色。路径为 mainBundle/图片名字.后缀*/
- NSString *imgName=@"icon.jpg";
- // NSString *imgpath=[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:imgName];
- // 与下面这句效果相同
- NSString *imgpath=[[NSBundle mainBundle] pathForResource:imgName ofType:nil];
- UIImage *image=[UIImage imageWithContentsOfFile:imgpath];
- /* 分组方式来使用图片 文件夹颜色为蓝色色。路径为 mainBundle/图片所在路径/图片名字.后缀*/
- // 使用另外一种方式来读取图片
- NSString *bundlePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Animations副本"];
- NSString *animPath = [bundlePath stringByAppendingPathComponent:imgName];
- UIImage *image = [UIImage imageWithContentsOfFile:animPath];
注: 有缓存的图片不能用无缓存的方式加载
plist:
一般可以使用属性列表文件存储NSArray或者NSDictionary之类的数据,这种属性列表文件的扩展名是plist,因此也成为“Plist文件”
- // 获得Plist文件的全路径
- NSBundle *bundle = [NSBundle mainBundle];
- NSString *path = [bundle pathForResource:@"plistName" ofType:@"plist"];
- //也可以
- //NSString *path = [bundle pathForResource:@"plistName.plist" ofType:nil];
加载plist文件
- NSArray pList=[NSArray arrayWithContentsOfFile:path];
加载后pList中的每个元素都会根据plist文件中给定的类型和数据来创建相应的元素。
一般使用plist文件加载后,会放如数据模型类中。方便提取数据
模型数据(例子)
- LFAppInfo.m
- #import "LFAppInfo.h"
- @implementation LFAppInfo
- -(instancetype)initWithPlist:(NSDictionary *)dict{
- self.icon=dict[@"icon"];
- self.name=dict[@"name"];
- return self;
- }
- +(instancetype)appInfoWithPlist:(NSDictionary *)dict{
- return [[self alloc] initWithPlist:dict];
- }
- @end
- LFAppInfo.m
- #import "LFAppInfo.h"
- @implementation LFAppInfo
- -(instancetype)initWithPlist:(NSDictionary *)dict{
- self.icon=dict[@"icon"];
- self.name=dict[@"name"];
- return self;
- }
- +(instancetype)appInfoWithPlist:(NSDictionary *)dict{
- return [[self alloc] initWithPlist:dict];
- }
- @end
懒加载:
懒加载主要就是2点:
1.写在get方法中(重写get方法)。
2.在get方法中,判断需要进行懒加载的变量,是否为nil
是,就加载。
否,就不需要加载。
此时,成员变量便只有在get方法调用时,加载数据。
之后再调用get方法时如果已经加载过数据了,就直接返回,不会重新再加载一次。
例如:
- - (NSArray *)images
- {
- if (_images == nil) {
- NSBundle *bundle = [NSBundle mainBundle];
- NSString *path = [bundle pathForResource:@"imageData" ofType:@"plist"];
- _images = [NSArray arrayWithContentsOfFile:path];
- }
- return _images;
- }
序列帧动画
1.判断是否在执行动画的过程中,如果是,则直接返回,不执行后面的操作
2.制作一个数组。里面存放所需要播放的所有图片(UIImage)。
3.设置动画使用的图片数组,播放的次数,播放的时候,
4.开始播放
- -(void)tomAnimation:(NSString *)img count:(int)count{
- if([self.tom isAnimating]) return;
- NSMutableArray *arrayImg=[NSMutableArray array];
- for(int i=0;i<count;i++){
- NSString *imgName=[NSString stringWithFormat:@"%@_%02d.jpg",img,i];
- // NSString *imgpath=[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:imgName];
- // 与下面这句效果相同
- NSString *imgpath=[[NSBundle mainBundle] pathForResource:imgName ofType:nil];
- UIImage *image=[UIImage imageWithContentsOfFile:imgpath];
- [arrayImg addObject:image];
- }
- [self.tom setAnimationImages:arrayImg];
- [self.tom setAnimationRepeatCount:1];
- [self.tom setAnimationDuration:arrayImg.count*0.075];
- [self.tom startAnimating];
- [self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration];
- }
添加动画效果
2种方式。
1.block方式(一般都使用这种方式)
- [UIView animateWithDuration:duration delay:0.0 options:7 << 16 animations:^{
- // 需要执行动画的代码
- } completion:^(BOOL finished) {
- // 动画执行完毕执行的代码
- }];
2. 普通方法
- [UIView beginAnimations:nil context:nil];
- [UIView setAnimationDuration:0.5];
- <span style="white-space:pre"> </span>
- <span style="white-space:pre"> </span>// 需要执行动画的代码
- <span style="white-space:pre"> </span>
- [UIView commitAnimations];
ios 图片的两种加载方式的更多相关文章
- 渐进式jpeg(progressive jpeg)图片及其相关 --图片的两种加载方式
渐进式jpeg(progressive jpeg)图片及其相关 一.基本JPEG(baseline jpeg)和渐进JPEG 网络上那些色色的照片都是.jpg格式的("色色"指 ...
- Linux共享库两种加载方式简述
Linux共享库两种加载方式简述 动态库技术通常能减少程序的大小,节省空间,提高效率,具有很高的灵活性,对于升级软件版本也更加容易.与静态库不同,动态库里面的函数不是执行程序本身 的一部分,而是 ...
- Xamarin Android Fragment的两种加载方式
android Fragment的重点: 3.0版本后引入,即minSdk要大于11 Fragment需要嵌套在Activity中使用,当然也可以嵌套到另外一个Fragment中,但这个被嵌套的Fra ...
- Linux驱动的两种加载方式过程分析
一.概念简述 在Linux下可以通过两种方式加载驱动程序:静态加载和动态加载. 静态加载就是把驱动程序直接编译进内核,系统启动后可以直接调用.静态加载的缺点是调试起来比较麻烦,每次修改一个地方都要重新 ...
- dll的两种加载方式(pend)+ delayload
看过关于动态库的调用例子,于是决定动手做一做:dll的对外接口声明头文件,Mydll.h: //Mydll.h #include <stdio.h> #include <stdlib ...
- Qml文件的两种加载方式
一种是QQmlApplicationEngine搭配Window,例如: #include <QGuiApplication> #include <QQmlApplicationEn ...
- Android Activity四种加载方式
Android之四种加载方式 (http://marshal.easymorse.com/archives/2950 图片) 在多Activity开发中,有可能是自己应用之间的Activity跳转,或 ...
- Android学习笔记_50_(转 四种加载方式详解(standard singleTop singleTask singleInstance)
Android之四种加载方式 (http://marshal.easymorse.com/archives/2950 图片) 在多Activity开发中,有可能是自己应用之间的Activity跳转,或 ...
- Android 四种加载方式详解(standard singleTop singleTask singleInstance) .
Android之四种加载方式 (http://marshal.easymorse.com/archives/2950 图片) 在多Activity开发中,有可能是自己应用之间的Activity跳转,或 ...
随机推荐
- 《Inside UE4》-0-开篇
<Inside UE4>-0-开篇 InsideUE4 前言 VR行业发展是越来越火热了,硬件设备上有HTC VIVE,Oculus rift,PS VR,各种魔镜:平台上有Steam ...
- .Net Task<T>的一种比较神奇的卡死情况(Wait/Result卡死, await能得到结果)
出现的环境.Net4.0 + WebApi1(4.0.30506.0) + Microsoft.Bcl.Async.1.0.168 自己死活看不出原因, 分享出来给大家看看,希望有人能找到问题的关键 ...
- Visual Studio 后期生成事件复制配置文件
命令行 copy 源文件 目标路径 copy "$(SolutionDir)\ICBC.Sdyf.Applications\bin\Debug\ICBC.Sdyf.Applications. ...
- 使用VS Code开发ASP.NET Core 应用程序
最新教程:http://www.cnblogs.com/linezero/p/VSCodeASPNETCore.html 使用VS Code开发ASP.NET Core 应用程序 准备 1.安装VS ...
- 有关微信开发中errorcode:-1 errmsg:system error 错误的一点原因
如果你在微信开发中遇到{"errcode":-1,"errmsg":"system error,hints:[req_id:]"}这样的错误 ...
- 为什么说Babel将推动JavaScript的发展
Babel是一个转换编译器,它能将ES6转换成可以在浏览器中运行的代码.Babel由来自澳大利亚的开发者Sebastian McKenzie创建.他的目标是使Babel可以处理ES6的所有新语法,并为 ...
- C# 操作鼠标移动到指定的屏幕位置方法
/// <summary> /// 引用user32.dll动态链接库(windows api), /// 使用库中定义 API:SetCursorPos /// </summary ...
- freemarker:简介
Apache FreeMarker模板引擎:Java库来生成文本输出(HTML网页,电子邮件,配置文件,源代码,等等)基于模板和变化的数据.模板都写在FreeMarker模板语言(FTL),这是一个简 ...
- android FragmentActivity+FragmentTabHost+Fragment框架布局
这周比较闲,计划系统的学习一下android开发,我本是一名IOS程序员,对手机开发还是有自己的一套思路的, 固这套思路用到我当前学android上了,先选择从Main页面的tabbar部分代码入手, ...
- socket.io,远程控制你的幻灯片
原文:http://www.cnblogs.com/xiezhengcai/p/3964455.html 中秋休息了几天,今天又开始捣鼓socket.io了.今天的任务是通过socket.io控制你的 ...