UIImageView动画组优化内存

2019-10-11  本文已影响0人  guoguojianshu
//注意抽取方法的原则
//把公共的内容放到方法中,把不同的当成参数
-(void)playAnimationWithName:(NSString *)fileName imgCount:(int)imgCount
{
    if (self.tomView.isAnimating) {
        //表明当前正在播放图片
        return ;//不再播放其他动画
    }
    NSMutableArray *arr6=[NSMutableArray array];
    for (int i=0; i<imgCount; i++) {
        NSString *file6=[NSString stringWithFormat:@"%@_%02d.jpg",fileName,i];
        NSString *path=[[NSBundle mainBundle]pathForResource:file6 ofType:nil ];
        UIImage *img=[UIImage imageWithContentsOfFile:path];
    
        //
        //UIImage *img6=[UIImage imageNamed:file6];//直接从bundle中加载图片
        //如果使用这种方法来加载图片,系统会自动缓存这些图片
        //不缓存图片
        [arr6 addObject:img];
    }
    self.tomView.animationRepeatCount=1;
    self.tomView.animationImages=arr6;
    self.tomView.animationDuration=4;
    [self.tomView startAnimating];
    //self.tomView.animationImages=nil;此方法延迟执行
    //动画
    //1.数组存放图片
    //2.把所有图片放到view
    //3.设置播放时间等,启动播放
    //延迟执行方法的方法
    //优化内存
    CGFloat time=self.tomView.animationRepeatCount*self.tomView.animationDuration;
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        self.tomView.animationImages=nil;//清除动画图片
//    });
    //第二中清除方法
    [self.tomView performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:time];//过段时间以后执行方法
   // [self.tomView setAnimationImages:nil];
    //必须使用imageWithContentOfFile来加载图片
    //当播放完所有的图片以后一定要清空uiimageview动画数组中所有图片
    
}
上一篇下一篇

猜你喜欢

热点阅读