美文网首页扩展iOS开发ios干货
iOS UIImage加载gif图片

iOS UIImage加载gif图片

作者: 铸造中 | 来源:发表于2019-08-09 17:39 被阅读0次

一.gif格式主要分块

(1) 控制块:控制块包含有用来控制数据流(Data Stream)或者设置硬件参数的信息,其成员包括:
GIF文件头(Header)
逻辑屏幕描述块(Logical Screen Descriptor)
图形控制扩展块(Graphic Control Extension):
描述了每一帧图片的延迟时间
文件结束块(Trailer)
(2) 图形描绘块:包含有用来描绘在显示设备上显示图形的信息和数据,其成员包括:
图像描述块(Image Descriptor):
GIF图像文件格式可包含数量不限的图像,而且也没有一个固定的存放顺序,仅用一个字节的图像分隔符来判断是不是图像描述块。每一幅图像都由一个图像描述块可有可无的局部彩色表和图像数据组成。每幅图像必须要落在逻辑屏幕描述块中定义的逻辑屏尺寸范围里。

无格式文本扩展块(Plain Text Extension)
(3) 特殊用途数据块;包含有与图像处理无关的信息,其成员包括:
注释扩展块(Comment Extension)
应用扩展块(Application Extension)

二.加载gif原理

解压gif图生成一个图片数组,生成animate image赋值给UIImageView。

//Creates and returns an animated image from an existing set of images.
+ (UIImage *)animatedImageWithImages:(NSArray<UIImage *> *)images duration:(NSTimeInterval)duration;

当UIMageView拿到这种类型的iamge,就会直接播放动销图片。
下面我们要做的就是,如何解压缩data,并且生成一个图片数组,根据数组生成animate iamge

1.data解压过程知识点

1.根据data数据获取SourceRef资源器,通过它可以操作gif的data数据拿到信息

CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, nil);

2.根据source可以拿到gif的图片数量 size_t 类似于一个无符号int

size_t count = CGImageSourceGetCount(source);

3根据指定的位置拿到指定顺序的iamgeRef,iamgeRef是图片画笔,有了它可以生成Image.

CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, 位置, NULL);

4.获取指定顺序gif图片的图片信息,比如宽、高、颜色等

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

5.指定获取gif的图像属性并且获取里面的图像持续时间 有了这个时间就可以计算出gif播放的总时间

NSDictionary * gifProperties = [imageProperties valueForKey:(NSString *)kCGImagePropertyGIFDictionary];
NSNumber * delayTime = [gifProperties valueForKey:(NSString *)kCGImagePropertyGIFUnclampedDelayTime];
2.代码实现
//解压gif的data为图片数组
-(UIImage *)decodeGifImageByData:(NSData *)data {
    //获取data资源器,这个可以直接操作图片data
    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, nil);
    //获取图片数量 size_t 类似于无符号int
    size_t count = CGImageSourceGetCount(source);
    UIImage * animationImage;
    
    //如果只有一张则直接解压
    if (count <= 1) {
        animationImage = [[UIImage alloc]initWithData:data];
    }
    else {
        NSMutableArray * imageArray = [NSMutableArray arrayWithCapacity:count];
        NSTimeInterval duration = 0.0f;
        //遍历获取每一张图片
        for (size_t i = 0; i < count; i++) {
            //解析图片拿到图片画笔拿到图片画笔
            CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, i, NULL);
            duration += [self imageDurationAtIndex:i source:source];
            //scale:图片缩放因子 默认1  orientation:图片绘制方向 默认网上
            [imageArray addObject:[UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];
            CGImageRelease(imageRef);
        }
        
        //如果没有抓取到图片播放时间,则按照0.1秒一张的方式来了播放
        if (!duration) {
            duration = (1.0f / 10.0f) * count;
        }
        animationImage = [UIImage animatedImageWithImages:imageArray duration:duration];
    }
    
    
    CFRelease(source);
    return animationImage;
}

//获取每一张图片的持续时间
-(float)imageDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source {
    float imageDuration = 0.1f;
    //获取指定图像的属性信息 如宽、高、持续时间等都在里面 详情参考 CGImageProperties
    CFDictionaryRef cfImageProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil);
    if (!cfImageProperties) {
        return imageDuration;
    }
    NSDictionary * imageProperties = (__bridge NSDictionary *) cfImageProperties;
    //拿到gif图的属性信息 获取每一帧的时间
    NSDictionary * gifProperties = [imageProperties valueForKey:(NSString *)kCGImagePropertyGIFDictionary];
    NSNumber * delayTime = [gifProperties valueForKey:(NSString *)kCGImagePropertyGIFUnclampedDelayTime];
    if (delayTime != nil) {
        return delayTime.floatValue;
    }
    
    return imageDuration;
}

需要demo可以加QQ群:839813029

相关文章

网友评论

    本文标题:iOS UIImage加载gif图片

    本文链接:https://www.haomeiwen.com/subject/hgahjctx.html