iOS 播放.GIF图片


- (void)downloadImages:(int)index {

   //下载图片

    NSBlockOperation *download = [NSBlockOperation blockOperationWithBlock:^{

        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];

        if (!data) return;

        UIImage *image = getImageWithData(data);

        //取到的data有可能不是图片

        if (image) {

            self.images[index] = image;

            //如果下载的图片为当前要显示的图片,直接到主线程给imageView赋值,否则要等到下一轮才会显示

            

            NSLog(@"images_index= %ld",index);


            if (_currIndex == index) [_currImageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];

            if (_autoCache) [data writeToFile:path atomically:YES];

        }

    }];

    [self.queue addOperation:download];



}


#pragma mark 下载图片,如果是gif则计算动画时长

UIImage *getImageWithData(NSData *data) {

    CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);

    size_t count = CGImageSourceGetCount(imageSource);

    if (count <= 1) { //gif

        CFRelease(imageSource);

        return [[UIImage alloc] initWithData:data];

    } else { //gif图片

        NSMutableArray *images = [NSMutableArray array];

        NSTimeInterval duration = 0;

        for (size_t i = 0; i < count; i++) {

            CGImageRef image = CGImageSourceCreateImageAtIndex(imageSource, i, NULL);

            if (!image) continue;

            duration += durationWithSourceAtIndex(imageSource, i);

            [images addObject:[UIImage imageWithCGImage:image]];

            CGImageRelease(image);

        }

        if (!duration) duration = 0.1 * count;

        CFRelease(imageSource);

        return [UIImage animatedImageWithImages:images duration:duration];

    }

}


#pragma mark 获取每一帧图片的时长

float durationWithSourceAtIndex(CGImageSourceRef source, NSUInteger index) {

    float duration = 0.1f;

    CFDictionaryRef propertiesRef = CGImageSourceCopyPropertiesAtIndex(source, index, nil);

    NSDictionary *properties = (__bridge NSDictionary *)propertiesRef;

    NSDictionary *gifProperties = properties[(NSString *)kCGImagePropertyGIFDictionary];

    

    NSNumber *delayTime = gifProperties[(NSString *)kCGImagePropertyGIFUnclampedDelayTime];

    if (delayTime) duration = delayTime.floatValue;

    else {

        delayTime = gifProperties[(NSString *)kCGImagePropertyGIFDelayTime];

        if (delayTime) duration = delayTime.floatValue;

    }

    CFRelease(propertiesRef);

    return duration;

}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值