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跳转,或 ...
随机推荐
- JS魔法堂:jQuery.Deferred(jQuery1.5-2.1)源码剖析
一.前言 jQuery.Deferred作为1.5的新特性出现在jQuery上,而jQuery.ajax函数也做了相应的调整.因此我们能如下的使用xhr请求调用,并实现事件处理函数晚绑定. var p ...
- 从设计到开发,硅谷技术专家教你做“声控”APP
编者:本文为携程机票研发部技术专家祁一鸣在携程技术微分享中的分享内容,关注携程技术中心微信公号ctriptech,获知更多一手干货. [携程技术微分享]是携程技术中心推出的线上公开分享课程,每月1-2 ...
- Swift 自定义Subscript
Swift可以方便给自定义类加下标,其中参数和返回值可以在类里定义为任意类型: subscript(parameters) -> ReturnType { get { //return some ...
- javascript(js)小数精度丢失的解决方案
原因:js按照2进制来处理小数的加减乘除,在arg1的基础上 将arg2的精度进行扩展或逆扩展匹配,所以会出现如下情况. javascript(js)的小数点加减乘除问题,是一个js的bug如0.3* ...
- JsonConvert 使用注意事项之 Serializable
1.使用NuGet安装Newtonsoft.Json. 2.创建需要序列化的类. public class Person { public string Name{get;set;} public i ...
- ScrollView与ListView冲突解决
正 常来说,在ScrollView添加一个ListView后在真机上只会显示ListView的一行多一点,我也不理解为什么会这样,后来我把 ListView的layout_height改成400dip ...
- 【C#进阶系列】16 数组
首先提一下,个人在项目中已经很少用到数组了,更多的时候使用List<>. 数组大小固定,如果只是用来存放数据,专门用来读取,更改当然方便.但是更多的时候我们需要进行增删改,这个时候用Lis ...
- hdu-1213-How Many Tables
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- jQuery Mobile页面返回无需重新get
最近公司的web app项目,使得我有幸一直接触和学习jQuery Mobile.这确实是一个很不错的移动开发库,有助于擅长web开发的工程师,快速入门并构建自己的移动应用.但是在前两天,我碰到了一个 ...
- Java在方法作用域内创建的内部类
在方法作用域内创建的内部类,用来实现一个接口 /** * Created by xfyou on 2016/11/3. * Java内部类演示 */ public class Parcel3 { pu ...