iOS 使用ImageIO framework解析gif

    技术2022-07-11  85

    ImageIO介绍

    概述: ImageIO框架提供了读取与写入图片数据的基本方法,使用它一、可以获取到图片元数据(描述图片属性的数据:宽、高、图片类型等等)。二、获取网络图片和上传图片到网络上。三、解析图片。四、实现图片的渐进加载主要类描述:

          CGImageSource:用来读取图片数据

          CGImageDestination:将图片数据写入指定的目标中

          CGImageMetadata:图片元数据封装

    gif解析步骤

     

    1.//转换url

    NSURL *gifURL = [NSURL fileURLWithPath:gifPath];

    CFURLRef urlRef = (__bridge CFURLRef)gifURL;

     2.// 获取图片Ref

    CGImageSourceRef sourceRef = CGImageSourceCreateWithURL(urlRef, NULL);

    3.// 获取图片类型

    CFStringRef typeRef = CGImageSourceGetType(sourceRef);

    CFStringRef gifRef = CFSTR("com.compuserve.gif");

    4.// 判断是否属于gif 类型

    CFComparisonResult result = CFStringCompare(typeRef, gifRef, kCFCompareCaseInsensitive);

    5.// 获取GIF帧数

    size_t count = CGImageSourceGetCount(sourceRef);

    6.// 获取帧间间隔

    CFDictionaryRef propertiesRef = CGImageSourceCopyPropertiesAtIndex(self.sourceRef, index, NULL);

    CFDictionaryRef gifPropertiesRef = CFDictionaryGetValue(propertiesRef, kCGImagePropertyGIFDictionary);

    CFStringRef timeRef = CFDictionaryGetValue(gifPropertiesRef, kCGImagePropertyGIFUnclampedDelayTime);

    CFRelease(propertiesRef);

    NSString *timeString = (__bridge NSString *)(timeRef);

    NSTimeInterval time = [timeString doubleValue];

    7.// 获取每一帧图片

    CGImageRef imageRef = CGImageSourceCreateImageAtIndex(self.sourceRef, index, NULL);

    Processed: 0.011, SQL: 9