iOS-使用系统类库加载gif格式图片

- (id)initWithFrame:(CGRect)frame filePath:(NSString *)_filePath{

    

    self = [super initWithFrame:frame];

    if (self) {

        

        /*

         *_filePath :    gif路径

         *data :    取得这个gif

         */

        NSData *data = [NSData dataWithContentsOfFile:_filePath];

        /*

         *gifLoopCount  : 设置一个gif的循环属性 ,值为0

         */

        NSDictionary *gifLoopCount = [NSDictionary dictionaryWithObjectsAndKeys:

                                      [NSNumber numberWithInt:0] , (NSString *)kCGImagePropertyGIFLoopCount,nil

                                      ];

        /*

         *创建gif属性

         */

        NSDictionary * gifProperties = [NSDictionary dictionaryWithObject:gifLoopCount forKey:(NSString *)kCGImagePropertyGIFDictionary] ;

        /*

         *根据属性 还有data 得到gif,并存在CGImageSourceRef

         *{

         *    ColorModel = RGB;

         *    Depth = 8;//

         *    HasAlpha = 1;

         *    PixelHeight = 22;

         *    PixelWidth = 22;

         *    "{GIF}" =     {

         *        DelayTime = "0.1";

         *        UnclampedDelayTime = "0.1";

         *    };

         *}

         */

        CGImageSourceRef gif = CGImageSourceCreateWithData((__bridge  CFDataRef)(data), (__bridge  CFDictionaryRef)gifProperties);

        CFDictionaryRef gifprops =(CGImageSourceCopyPropertiesAtIndex(gif,0,NULL));

        /*

         *count :  gif的张数

         */

        NSInteger count =CGImageSourceGetCount(gif);

        

        CFDictionaryRef  gifDic = CFDictionaryGetValue(gifprops, kCGImagePropertyGIFDictionary);

        /*

         *delay:    延迟时间??

         */

        NSNumber * delay = CFDictionaryGetValue(gifDic, kCGImagePropertyGIFDelayTime);

        /*

         *unclampedDelay:    延迟时间??

         */

        NSNumber * unclampedDelay = CFDictionaryGetValue(gifDic, kCGImagePropertyGIFUnclampedDelayTime);

        

        //注:本人不是做美术的,不懂gif原理,但经过我试验, DelayTimeUnclampedDelayTime 应该是取UnclampedDelayTime做出来的图才和用浏览器打开的时间相同

        

        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:delay.floatValue target:self selector:@selector(play) userInfo:nil repeats:YES];

        [timer fire];

        

        CFRelease(gifprops);

        CFRelease(gif);

    }

}


-(void)play

{

    index ++;

    index = index%count;

    CGImageRef ref = CGImageSourceCreateImageAtIndex(gif, index, (CFDictionaryRef)gifProperties);

    self.layer.contents = (id)ref;

    CFRelease(ref);

}

-(void)removeFromSuperview

{

    NSLog(@"removeFromSuperview");

    [timer invalidate];

    timer = nil;

    [super removeFromSuperview];

}

- (void)dealloc {

    NSLog(@"dealloc");

    CFRelease(gif);

    [gifProperties release];

    [super dealloc];

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值