• ios 图片的两种加载方式


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

    IOS中有2种加载图片的方式、
    方式一:有缓存(图片所占用的内存会一直停留在程序中)

    1. + (UIImage *)imageNamed:(NSString *)name;  
    注:必须是png文件。需要把图片添加到 images.xcassets中


    例如:
    1. @property (weak, nonatomic) IBOutlet UIImageView *iconImageView;  
    2.   
    3.     self.iconImageView.image=[UIImage imageNamed:@"icon"];  

    方式二:无缓存(图片所占用的内存会在一些特定操作后被清除)
    .jpg格式的图片只能用无缓存方式加载
    1. + (UIImage *)imageWithContentsOfFile:(NSString *)path  
    2. - (id)initWithContentsOfFile:(NSString *)path;  


    path是图片的全路径
        其中又有:分组方式使用图片和不分组方式使用图片
        分组方式导入的图片是蓝色的文件夹,创建UIImage时,需要完整的路径名(mainBundle +图片名字)
        不分组方式导入的图片是黄色的文件夹,创建UIImage时,不需要完整的路径名(mianBundle+路径名+图片名字)


    例如:
    1.  /* 不分组方式来使用图片 文件夹颜色为黄色。路径为 mainBundle/图片名字.后缀*/  
    2. NSString *imgName=@"icon.jpg";  
    3.   
    4.       // NSString *imgpath=[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:imgName];  
    5.       // 与下面这句效果相同  
    6.       NSString *imgpath=[[NSBundle mainBundle] pathForResource:imgName ofType:nil];  
    7.   
    8. UIImage *image=[UIImage imageWithContentsOfFile:imgpath];  
    9.   
    10.   /* 分组方式来使用图片 文件夹颜色为蓝色色。路径为 mainBundle/图片所在路径/图片名字.后缀*/  
    11. // 使用另外一种方式来读取图片  
    12. NSString *bundlePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Animations副本"];  
    13. NSString *animPath = [bundlePath stringByAppendingPathComponent:imgName];  
    14.   
    15.       UIImage *image = [UIImage imageWithContentsOfFile:animPath];  

    注: 有缓存的图片不能用无缓存的方式加载

    plist:
    一般可以使用属性列表文件存储NSArray或者NSDictionary之类的数据,这种属性列表文件的扩展名是plist,因此也成为“Plist文件”

    1. // 获得Plist文件的全路径  
    2. NSBundle *bundle = [NSBundle mainBundle];  
    3. NSString *path = [bundle pathForResource:@"plistName" ofType:@"plist"];  
    4. //也可以  
    5. //NSString *path = [bundle pathForResource:@"plistName.plist" ofType:nil];  

    加载plist文件
    1. NSArray pList=[NSArray arrayWithContentsOfFile:path];  

    加载后pList中的每个元素都会根据plist文件中给定的类型和数据来创建相应的元素。


    一般使用plist文件加载后,会放如数据模型类中。方便提取数据


    模型数据(例子)
    1. LFAppInfo.m  
    2.   
    3. #import "LFAppInfo.h"  
    4.   
    5. @implementation LFAppInfo  
    6. -(instancetype)initWithPlist:(NSDictionary *)dict{  
    7.     self.icon=dict[@"icon"];  
    8.     self.name=dict[@"name"];  
    9.     return self;  
    10. }  
    11.   
    12. +(instancetype)appInfoWithPlist:(NSDictionary *)dict{  
    13.     return [[self alloc] initWithPlist:dict];  
    14. }  
    15. @end  

    1. LFAppInfo.m  
    2. #import "LFAppInfo.h"  
    3.   
    4. @implementation LFAppInfo  
    5. -(instancetype)initWithPlist:(NSDictionary *)dict{  
    6.     self.icon=dict[@"icon"];  
    7.     self.name=dict[@"name"];  
    8.     return self;  
    9. }  
    10.   
    11. +(instancetype)appInfoWithPlist:(NSDictionary *)dict{  
    12.     return [[self alloc] initWithPlist:dict];  
    13. }  
    14. @end  

    懒加载:
    懒加载主要就是2点:
    1.写在get方法中(重写get方法)。
    2.在get方法中,判断需要进行懒加载的变量,是否为nil
    是,就加载。
    否,就不需要加载。


    此时,成员变量便只有在get方法调用时,加载数据。
    之后再调用get方法时如果已经加载过数据了,就直接返回,不会重新再加载一次。






    例如:
    1. - (NSArray *)images  
    2. {  
    3.     if (_images == nil) {  
    4.         NSBundle *bundle = [NSBundle mainBundle];  
    5.         NSString *path = [bundle pathForResource:@"imageData" ofType:@"plist"];  
    6.         _images = [NSArray arrayWithContentsOfFile:path];  
    7.     }  
    8.     return _images;  
    9. }  

    序列帧动画
       1.判断是否在执行动画的过程中,如果是,则直接返回,不执行后面的操作
            2.制作一个数组。里面存放所需要播放的所有图片(UIImage)。
            3.设置动画使用的图片数组,播放的次数,播放的时候,
            4.开始播放
    1. -(void)tomAnimation:(NSString *)img count:(int)count{         
    2.     if([self.tom isAnimating])  return;  
    3.       
    4.     NSMutableArray *arrayImg=[NSMutableArray array];  
    5.   
    6.     for(int i=0;i<count;i++){  
    7.             NSString *imgName=[NSString stringWithFormat:@"%@_%02d.jpg",img,i];  
    8.           
    9.             // NSString *imgpath=[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:imgName];  
    10.             // 与下面这句效果相同  
    11.             NSString *imgpath=[[NSBundle mainBundle] pathForResource:imgName ofType:nil];  
    12.           
    13.             UIImage *image=[UIImage imageWithContentsOfFile:imgpath];  
    14.             [arrayImg addObject:image];  
    15.     }     
    16.       
    17.     [self.tom setAnimationImages:arrayImg];  
    18.     [self.tom setAnimationRepeatCount:1];  
    19.     [self.tom setAnimationDuration:arrayImg.count*0.075];  
    20.     [self.tom startAnimating];  
    21.     [self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration];  
    22.  }  
    23.    

     
     添加动画效果
     2种方式。


    1.block方式(一般都使用这种方式)

    1. [UIView animateWithDuration:duration delay:0.0 options:7 << 16 animations:^{  
    2.     // 需要执行动画的代码  
    3.      
    4. } completion:^(BOOL finished) {  
    5.     // 动画执行完毕执行的代码  
    6. }];  

    2. 普通方法

    1.     [UIView beginAnimations:nil context:nil];  
    2.     [UIView setAnimationDuration:0.5];  
    3. <span style="white-space:pre">    </span>  
    4. <span style="white-space:pre">    </span>// 需要执行动画的代码  
    5. <span style="white-space:pre">    </span>  
    6.     [UIView commitAnimations]; 

    您的资助是我最大的动力!
    金额随意,欢迎来赏!

    如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的推荐按钮。
    如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的关注我

    如果,想给予我更多的鼓励,求打

    因为,我的写作热情也离不开您的肯定支持,感谢您的阅读,我是【往事亦如风】!

  • 相关阅读:
    idea从零搭建简单的springboot+Mybatis
    关于监听微服务功能
    final
    mysql数据库分页查询优化
    Mysql中“select ... for update”排他锁(转)
    HashMap(常用)方法个人理解
    5.1 Object类型
    5.3 Date类型
    4.1-4.2 基本类型及引用类型,执行环境及作用域
    20.JSON
  • 原文地址:https://www.cnblogs.com/ldnh/p/5270135.html
Copyright © 2020-2023  润新知