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跳转,或 ...
随机推荐
- 通过自定义相册来介绍photo library的使用
因为我在模仿美图秀秀的功能,在使用相册时候,UIImagePickerController本来就是一个UINavigationController的子类,所以没有办法使用push,所以做了一个自定义的 ...
- HTML基础—插曲
HTML基础学习 1:我们在网上添加图片的时候最好是缩略图,而不是直接在代码中限制图片的大小.可以为了用户减少流量.Alt=""属性是为了让图片在现实不出来时显示的文字,Title ...
- Bootstrap学习笔记系列2-------Bootstrap简单表格处理
标签 <table> 为表格添加基础样式 <thead> 表格标题行的容器元素,用来识别列 <tbody> 表格主提中的表格行的容器元素 <tr> 单行 ...
- js操纵css更改加载图片大小
- all requires API level 3 (current min is 1)问题的解决
几次出现了all requires API level 3 (current min is 1)的错误,后来发现解决的方法是右键单击项目文件夹,选择Android Tools->Clear Li ...
- request的各种方法大全
request.setAttribute("result", district);//放进作用域,可以用el表达式在页面展示.(名字,调用方法的结果) pageContext.re ...
- 【算法和数据结构】_11_小算法_itoa、ftoa及字符串倒置
[1]main.c /**************************************************** * * 把整数按照进制数转换为相应进制的字符串 *(要考虑符号),比如 ...
- 简单理解——面向切面编程(AOP)
在传统的编写业务逻辑处理代码时,我们通常会习惯性地做几件事情:日志记录.事务控制及权限控制等,然后才是编写核心的业务逻辑处理代码.当代码编写完成回头再看时,不禁发现,扬扬洒洒上百行代码中,真正用于核心 ...
- spring的使用《一》
在前边的文章中说明了,如何搭建一个spring的开发环境,简单回顾下就是把spring的jar包导入工程中,如果是在javaWeb项目中是放在lib目录下,然后在web.xml文件中进行配置,配置sp ...
- Java经典实例:使用DateFormatter来格式化日期时间
Java版本:1.8开始 import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; ...