GIF

GIF动画处理
本文介绍如何将GIF动画分解为单帧图片,并重新合成GIF动画。通过Swift代码实现GIF的读取、分解及合成过程,适用于iOS平台。

GIF

主要内容来自《iOS动画核心技术与案例实战》

GIF分解

GIF分解为单帧图片的步骤如下:

1.本地读取GIF,将其装换为NSData数据类型
2.将NSData作为ImageIO模块的输入
3.获取ImageIO的输出数据:UIImage
4.将获取到的UIImage数据存储为JPG或PNG格式保存到本地

        //读取gif文件,转换为NSData类型
        let gifPath:NSString = Bundle.main.path(forResource: "plane", ofType: "gif")! as NSString
        let gifData:Data = try! Data(contentsOf: URL(fileURLWithPath: gifPath as String))
        //将原始数据类型NSData转换为ImageIO可以直接处理的数据类型
        let gifDataSource:CGImageSource = CGImageSourceCreateWithData(gifData as CFData, nil)!
        //获取gif图片的分帧个数
        let gifImageCount:Int = CGImageSourceGetCount(gifDataSource)
        //遍历
        for i in 0...gifImageCount-1{
            let imageref:CGImage? = CGImageSourceCreateImageAtIndex(gifDataSource, i, nil)
            let image:UIImage = UIImage(cgImage: imageref!, scale:UIScreen.main.scale, orientation:UIImageOrientation.up )
            let imageData:Data = UIImagePNGRepresentation(image)!
            var docs=NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
            let documentsDirectory = docs[0] as String
            let imagePath = documentsDirectory+"/\(i)"+".png"
            try? imageData .write(to: URL(fileURLWithPath: imagePath), options: [.atomic])
            print("\(imagePath)")

        }

CGImageSourceCreateImageAtIndex方法返回gif其中某一帧图片的CGImage类型数据

GIF合成

GIF合成过程:

1.加载待处理的图片
2.构建gif文件
3.设置gif文件属性,利用ImageIO编码gif文件

        // Part1:读取67张png图片
        let images:NSMutableArray = NSMutableArray()
        for i in 0...66{// 遍历本地67张图片
            let imagePath = "\(i).png" // 构建图片名称
            let image:UIImage = UIImage(named: imagePath)!//构建UIImage
            images.add(image)// 将图片添加到数组中
        }
        // Part2:在Document目录创建gif文件
        var docs=NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
        let documentsDirectory = docs[0] as String
        let gifPath = documentsDirectory+"/plane.gif"// 构建Doc目录下gif文件路径
        print("\(gifPath)")
        let url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, gifPath as CFString!, CFURLPathStyle.cfurlposixPathStyle, false)// string->CFURL
        let destion = CGImageDestinationCreateWithURL(url!, kUTTypeGIF, images.count, nil)

        // Part3:设置gif图片属性,利用67张png图片构建gif
        let cgimagePropertiesDic = [kCGImagePropertyGIFDelayTime as String:0.1]//设置每帧之间播放时间
        let cgimagePropertiesDestDic = [kCGImagePropertyGIFDictionary as String:cgimagePropertiesDic];//gif图片属性字典
        for cgimage in images{
            CGImageDestinationAddImage(destion!, (cgimage as AnyObject).cgImage!!,cgimagePropertiesDestDic as CFDictionary?);
        }// 依次为gif图像对象添加每一帧元素

        let gifPropertiesDic:NSMutableDictionary = NSMutableDictionary()
        gifPropertiesDic.setValue(kCGImagePropertyColorModelRGB, forKey: kCGImagePropertyColorModel as String)// 设置图像的彩色空间格式
        gifPropertiesDic.setValue(16, forKey: kCGImagePropertyDepth as String)// 设置图像的颜色深度
        gifPropertiesDic.setValue(1, forKey: kCGImagePropertyGIFLoopCount as String)// 设置Gif执行次数
        let gifDictionaryDestDic = [kCGImagePropertyGIFDictionary as String:gifPropertiesDic]
        CGImageDestinationSetProperties(destion!,gifDictionaryDestDic as CFDictionary?);//为gif图像设置属性
        CGImageDestinationFinalize(destion!);

CGImageDestinationCreateWithURL(url!, kUTTypeGIF, images.count, nil)方法的作用是创建一个图片的目标对象

  • 参数1表示图片的文件路径
  • 参数2描述图片的类型为gif图片
  • 参数3表明gif图片的帧数

GIF展示

UIImageView

        var images:[UIImage] = []
        for i in 0...66{// 遍历本地67张图片
            let imagePath = "\(i).png" // 构建图片名称
            let image:UIImage = UIImage(named: imagePath)!//构建UIImage
            images.append(image)// 将图片添加到数组中
        }
        let imageView = UIImageView()
        imageView.frame = self.view.bounds
        imageView.contentMode = UIViewContentMode.center
        self.view.addSubview(imageView)

        imageView.animationImages = images
        imageView.animationDuration = 5
        imageView.animationRepeatCount = 1
        imageView.startAnimating()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值