• 如何自动播放一段序列帧图片封装接口[原创<转载请说明出处谢谢>]


    在做iPhone或iPad应用程序的时候会经常涉及到如何去播放一段序列帧,这东西经常会用到参考书<ios软件开发揭秘>上提到了一种播放序列帧的方法我之前页尝试封装过一个类出来,但是在实际用法前面那种还不是很好用。主要是序列帧会一直占用内存导致程序会因为序列帧太多而崩溃,现在这里发出来的这个类主要采用定时器去播放序列帧,结束方法也有两种选择:一种是停止在最后一帧的图片上、一种是停止后直接将这个序列帧整个清空掉让其消失。现在虽然还不能达到进行逆序播放的效果,但是我相信用到这个类的同学们都会根据自己的实际情况进行优化扩展。

    作为一个菜鸟希望老鸟们多指点一下此类中不足之处:

    以下是类代码以及如何调用:

    //这里是头文件部分
    
    #import <UIKit/UIKit.h>
    
    @interface TimerSequence : UIImageView
    {
        NSString *imagePathOne;
        NSString *imagePathTwo;
        NSTimer *sequenceTimer;
        int sequenceNumber;
        int Record;
    }
    @property(nonatomic,copy) NSString *imagePrefix;  //图片名前缀
    @property(nonatomic,copy) NSString *type;         //图片类型
    @property(nonatomic,assign) int imageTotalNumber; //图片总数量<用于计算每帧停留时间>
    @property(nonatomic,assign) int numberOfCycles;   //循环播放周期次数<用于调节播放次数>
    @property(nonatomic,assign) float totalTime;      //序列帧播放总时间<用于调节序列帧播放速度>
    @property(nonatomic,assign) BOOL OverBool;        //选择结束方案
    //类接口如下:图片名前缀、图片类型、序列帧播放总时间、序列帧总数量、循环播放周期次数、选择结束方案
    -(TimerSequence*)initWithPrefix:(NSString*)_imagePrefix Type:(NSString*)_type TotalTime:(float)_totalTime ImageTotalNuber:(int)_imageTotalNumber NumberOfCycles:(int)_numberOfCycles OverTap:(BOOL)overBol;
    //定时器调用播放序列帧的方法
    -(void)timerToggleImage;
    //结束循环销毁定时器方法1
    -(void)timerOverOne;
    //结束循环销毁定时器方法2
    -(void)timerOverTwo;
    @end
    

      

    //点M文件部分
    
    #import "TimerSequence.h"
    
    @implementation TimerSequence
    @synthesize imagePrefix,type,imageTotalNumber,totalTime,numberOfCycles,OverBool;
    //类接口如下:图片名前缀、图片类型、序列帧播放总时间、序列帧总数量、循环播放周期次数、选择结束方案
    -(TimerSequence*)initWithPrefix:(NSString*)_imagePrefix Type:(NSString*)_type TotalTime:(float)_totalTime ImageTotalNuber:(int)_imageTotalNumber NumberOfCycles:(int)_numberOfCycles OverTap:(BOOL)overBol{
        self = [super init];
        if (!self) {
            return nil;
        }
        imagePrefix = _imagePrefix;
        type = _type;
        totalTime = _totalTime;
        imageTotalNumber = _imageTotalNumber;
        numberOfCycles = _numberOfCycles;
        OverBool = overBol;
        
        imagePathOne = [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"%@%03d",imagePrefix,0] ofType:type];
        self.image = [UIImage imageWithContentsOfFile:imagePathOne];
        
        sequenceNumber = 0;
        sequenceTimer = [NSTimer scheduledTimerWithTimeInterval:totalTime/imageTotalNumber target:self selector:@selector(timerToggleImage) userInfo:nil repeats:YES];
        return self;
    }
    //定时器调用播放序列帧的方法
    -(void)timerToggleImage{
        if (sequenceNumber < imageTotalNumber) {
            sequenceNumber = sequenceNumber + 1;
        }
        if (sequenceNumber == imageTotalNumber) {
            sequenceNumber = 0;
            Record = Record + 1;
            if (Record == numberOfCycles) {
                if (OverBool == NO) {
                    [self timerOverOne];
                }else{
                    [self timerOverTwo];
                }
            }
        }
        imagePathTwo = [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"%@%03d",imagePrefix,sequenceNumber] ofType:type];
        self.image = [UIImage imageWithContentsOfFile:imagePathTwo];
    }
    //结束循环销毁定时器
    -(void)timerOverOne{
        [sequenceTimer invalidate];
        sequenceTimer = nil;
    //    第一种隐藏序列帧的方法
    //    super.self.hidden = YES;
    //    第二种隐藏序列帧的方法
        [super.self removeFromSuperview];
    }
    //结束循环销毁定时器方法2
    -(void)timerOverTwo{
        [sequenceTimer invalidate];
        sequenceTimer = nil;
    }
    @end
    

      

    //这里请各自需要分拆到程序各部分去。类中已经说明接口方法
        [self.view addSubview:TimerView];
        tiec = [[TimerSequence alloc]initWithPrefix:@"xxx_0" Type:@"jpg" TotalTime:3 ImageTotalNuber:81 NumberOfCycles:2 OverTap:NO];
        tiec.frame = CGRectMake(0, 0, 1024, 768);
        [TimerView addSubview:tiec];
    

      

    吾屋无污,吴捂屋污,诬吾污屋,吾晤吴污,吾无物污,吴侮污吾,吾忤吴屋,悟吴诬吾,污屋吴污,吴侮吾污,吾勿武侮,吾唔吴误,诬侮污吾,吾屋悟忤.
  • 相关阅读:
    《运营笔记》:主要是猫扑论坛运营经验。3星。
    《澄明之境》:二十年期货交易员的经验:投资没有圣杯,控制风险,在市场阶梯式上升过程中赚钱。3星
    《好好说话》:常见沟通场景的应对误区与应答思路、应答句式。4星。
    《尖叫感》:是近20-30年来尤其是近3-5年来的优秀广告文案的整理汇编。3星
    《石油,用得完吗》:石油会比较缓慢地被替代。3星
    Discuz常见大问题-如何自定义单个页面
    Discuz常见大问题-如何DIY一个独立页面
    Discuz常见大问题-如何使用云采集插件
    Discuz常见大问题-如何使用图片轮播器
    Discuz常见大问题-如何在自定义页面使用首页四格
  • 原文地址:https://www.cnblogs.com/chenxiangxi/p/2804558.html
Copyright © 2020-2023  润新知