• IOS中的TOM(解决缓存,图片加载)


    
    
    #import "ViewController.h"
    
    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UIImageView *imageView;
    - (IBAction)drink;
    - (IBAction)tou;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.imageView.image = [UIImage imageNamed:@"angry_00.jpg"];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (void)anAnimating:(int)num name:(NSString *)name{
        if(self.imageView.isAnimating) return;
        NSMutableArray *images = [NSMutableArray array];
        //for循环读出所有的的文件名
        for (int i = 0; i < num; i++) {
            //占位符的妙用,%2d 表示占两位,不足的用空格补上
            //%02d表示占两位不足的用0补上
            
            NSString *filename = [NSString stringWithFormat:@"%@_%02d.jpg",name,i];
            //有缓存
            // UIImage *image = [UIImage imageNamed:filename];
            //获得文件的全路径
            //没有缓存
            NSBundle *bundle = [NSBundle mainBundle];
            NSString *path = [bundle pathForResource:filename ofType:nil];
            UIImage *image = [UIImage imageWithContentsOfFile:path];
            
            [images addObject:image];
            
        }
        NSLog(@"%@",images);
        //imageView的动态方法animationImages加载动画
        //animationRepeatCount动画执行的趟数
        //animationDuration动画的延迟时间
        //startAnimating动画开始
        //- (void)stopAnimating;停止动画
        //- (BOOL)isAnimating;动画是否在执行
        //imageview占内存
        self.imageView.animationImages = images;
        
        self.imageView.animationRepeatCount = 1;
        self.imageView.animationDuration = images.count*0.05;
        [self.imageView startAnimating];
        
        
        //直接写这导致动画还没放完就清空了
        //设置一个定时器
        //self.imageView.animationImages = nil;
        //设置定时器
        //withObject  表示要传的参数
        CGFloat delay = self.imageView.animationDuration + 1.0;
        [self performSelector:@selector(clearCache) withObject:nil afterDelay:delay];
    
    }
    
    - (IBAction)drink {
        //knockout
        [self anAnimating:81 name:@"drink"];
    }
    - (void)clearCache
    {
        self.imageView.animationImages = nil;
    }
    //解决缓存问题,当上一个动画没有执行完,会把上一个动画放到缓存区,占用资源,解决这个问题就是释放上次的动作资源
    //缓存的好处就是速度快
    - (IBAction)tou {
        [self anAnimating:81 name:@"knockout"];
    
    }
    @end
  • 相关阅读:
    快速排序最新理解
    八大排序算法
    图的创建
    ARM入门实践(一)----Mini6410上最简单的LED点灯裸机程序
    驱动编程思想之初体验 --------------- 嵌入式linux驱动开发之点亮LED
    嵌入式Linux LED小灯点亮实验
    手算KMP匹配的Next值和Nextval值
    及格率 不谢 cast(cast (sum(case when res>=60 then 1 else 0 end)*100/(count(1)*1.0) as float) as nvarchar)+'%' '及格率'
    Hibernate反向工程生成DAO
    struts2&&Hibernate Demo1
  • 原文地址:https://www.cnblogs.com/wohaoxue/p/4738396.html
Copyright © 2020-2023  润新知