美文网首页
关于 CGImageSource详解

关于 CGImageSource详解

作者: 海笙樾 | 来源:发表于2018-04-28 10:51 被阅读0次

    CGImageSource初步了解

    是图片的数据读取类,常见的UIImage本质只有图像数据,而图片是包含好多信息的,比如图片的时间、尺寸、位置等,有时候还要处理GIF,是一个整合了图片所有信息的类,例如你需要处理GIF图片,展示就需要用到这个类,得到每一帧的图片以及各帧图片之间的延迟然后进一步处理。

    获取图片源的一张图片示例

    NSURL * url = [NSURL fileURLWithPath:@"图片路径"];
    CGImageRef myImage = NULL;
    CGImageSourceRef myImageSource;
    //获取并创建CGImageSource对象
    myImageSource = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
    //从图片信息里获取一张图片(第0张)
    myImage = CGImageSourceCreateImageAtIndex(myImageSource,
                                              0,
                                              NULL);
    CFRelease(myImageSource);
    [UIImage imageWithCGImage:myImage];//得到的图片,返回的是UIImage类型
    

    处理GIF的思路

    @property (nonatomic, strong, readonly) __attribute__((NSObject)) CGImageSourceRef imageSource;
    //得到GIF的Data数据
    _imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)data,
                                                       (__bridge CFDictionaryRef)@{(NSString *)kCGImageSourceShouldCache: @NO});
    //判断是否是GIF图片
    CFStringRef imageSourceContainerType = CGImageSourceGetType(_imageSource);
            BOOL isGIFData = UTTypeConformsTo(imageSourceContainerType, kUTTypeGIF);
    //得到存放图片信息的字典
    NSDictionary *imageProperties = (__bridge_transfer NSDictionary *)CGImageSourceCopyProperties(_imageSource, NULL);
    //得到帧图像的图片个数
    size_t imageCount = CGImageSourceGetCount(_imageSource);
    for (size_t i = 0; i < imageCount; i++) {
            CGImageRef frameImageRef = CGImageSourceCreateImageAtIndex(_imageSource, i, NULL);
            if (frameImageRef) {
    //得到每一帧的图片
                        UIImage *frameImage = [UIImage imageWithCGImage:frameImageRef];
    NSDictionary *frameProperties = (__bridge_transfer NSDictionary *)CGImageSourceCopyPropertiesAtIndex(_imageSource, i, NULL);
                            NSDictionary *framePropertiesGIF = [frameProperties objectForKey:(id)kCGImagePropertyGIFDictionary];
                            
                            //得到每一帧的延迟
                            NSNumber *delayTime = [framePropertiesGIF objectForKey:(id)kCGImagePropertyGIFUnclampedDelayTime];
          }
    }
    //剩下的就是处理显示每一帧的Image和处理Image显示中间的的延迟,详情FLAnimatedImage已经实现了GIF播放功能,实现逻辑和这个差不多,鄙人不才,拙文若有不妥还望指教
    

    相关文章

      网友评论

          本文标题:关于 CGImageSource详解

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