ios控件加载图片,plist,懒加载,序列帧动画,添加动画效果。

控件加载图片,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];


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dwt1220

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值