zoukankan      html  css  js  c++  java
  • iOS 分析一个支持GIF的UIImage扩展:SwiftGIF

    Github:https://github.com/bahlo/SwiftGif

    这个extension代码不多,主要通过Apple的ImageIO框架进行解析GIF。

    整个扩展最核心还是下面的函数,搞了半天还是Apple的UIImage的类函数。所以重点就是找到GIF中的每一帧图片和每一帧的延迟是多少。

    只要通过该函数返回的UIImage就是一个带动画的UIImage(记得我当年切了几个图还新建NSTimer实现动画…)

    可见会调用API的重要性…

    let animation = UIImage.animatedImageWithImages(frames,duration: Double(duration) / 1000.0)

    大致过程如下:

    1.imageName.GIF文件 -> NSData

    contentsOfURL函数:

    2.NSdata -> CGImageSource

    CGImageSourceCreateWithData函数

    3.从CGImageSource的对象中取得图片张数

    CGImageSourceGetCount(source)

    4.创建CGImage类型的数组,将CGImageSource中的图片一张张的添加到该数组中

    CGImageSourceCreateImageAtIndex(source, i, nil)

    5.创建delay数组,将CGImageSource中的每一帧的图片的延迟加入数组,这个函数比较麻烦

    先是取得CGImageSource中的某个实体,类型是CFDictionary:

            let cfProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil)

    然后取得该字典中的Gif类型的字典,类型是CFDictionaryRef(CFDictionary的引用,是一样的)

        let gifProperties: CFDictionaryRef = unsafeBitCast(

                CFDictionaryGetValue(cfProperties,

                    unsafeAddressOf(kCGImagePropertyGIFDictionary)),

                CFDictionary.self)
    然后从GIF字典中取得延迟时间,这里取2次,先是尝试了kCGImagePropertyGIFUnclampedDelayTime,再尝试kCGImagePropertyGIFDelayTime:

    var delayObject: AnyObject = unsafeBitCast(

                CFDictionaryGetValue(gifProperties,

                    unsafeAddressOf(kCGImagePropertyGIFUnclampedDelayTime)),

                AnyObject.self)

    if delayObject.doubleValue == 0 {

                delayObject = unsafeBitCast(CFDictionaryGetValue(gifProperties,

                    unsafeAddressOf(kCGImagePropertyGIFDelayTime)), AnyObject.self)

            }

    6.这样大概就按顺序构建起了2个数组,一个是CGImage数组,一个是延迟大小。

    7.然后把CGImage的数组转成UIImage的数组,因为UIImage.animatedImageWithImages(frames,duration)需要传入的frames是IImage的数组。

    从中我感受到,CoreImage、CoreAnimation等图像有关的框架非常强大,其中有很多useful的API

  • 相关阅读:
    WPF 使用 Direct2D1 画图 绘制基本图形
    WPF 使用 Direct2D1 画图 绘制基本图形
    dot net core 使用 IPC 进程通信
    dot net core 使用 IPC 进程通信
    win2d 图片水印
    win2d 图片水印
    Java实现 LeetCode 240 搜索二维矩阵 II(二)
    PHP closedir() 函数
    PHP chroot() 函数
    PHP chdir() 函数
  • 原文地址:https://www.cnblogs.com/rayshen/p/5306590.html
Copyright © 2011-2022 走看看