IOS gif图片播放 swift

IOS gif图片播放 swift

    • 1. GIF在iOS平台上的几种加载方式
    • 2. 第三方开源框架播放GIF
      • 1. SDWebImage播放:
      • 2. Kingfisher播放:
    • 3. 自己实现加载GIF
      • 3.1 GIF的分解
      • 3.2 简单GIF拆解后通过动画播放
      • 3.3 封装GIF播放UIImage分类扩展

1. GIF在iOS平台上的几种加载方式

  • 使用DispatchSource创建定时器播放gif图
  • 使用UIImageView直接展示
  • 基于Timer定时器的逐帧动画效果
  • 基于CADisplaylink的逐帧动画效果
  • 使用WebView直接加载gif图

2. 第三方开源框架播放GIF

1. SDWebImage播放:

  1. SDWebImage方法:
      let imageView = UIImageView(frame: CGRect(x:0, y:0, width: BGVIEW_WIDTH, height: BGVIEW_WIDTH))    
       imageView.image=UIImage.sd_animatedGIF(with: gifDataas!Data)

2. Kingfisher播放:

  1. Kingfisher(Kingfisher源码下载):直接用加载网络的方法加载即可
 let path =Bundle.main.path(forResource:"loading", ofType:"gif")       
   let image =UIImageView(frame: CGRect(x:0, y:0, width: BGVIEW_WIDTH, height: BGVIEW_WIDTH))        
   image.kf.setImage(with:ImageResource(downloadURL:URL(fileURLWithPath: path!)))

3. 自己实现加载GIF

3.1 GIF的分解

GIF分解分为几个步骤:

  1. 将GIF图片转为Data
  2. 使用ImageIO根据Data获取图片的帧数,时间间隔等信息
  3. 根据时间播放图片
    IOS gif图片播放 swift_第1张图片

3.2 简单GIF拆解后通过动画播放

  1. swift 播放代码如下:
import UIKit
import ImageIO
 
class ViewController: UIViewController {
 
    @IBOutlet weak var imageView: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 加载Gif图片, 并且转成Data类型,"my.gif就是gif图片"
        guard let path = Bundle.main.path(forResource: "my.gif", ofType: nil) else { return }
        guard let data = NSData(contentsOfFile: path) else { return }
        
        // 从data中读取数据: 将data转成CGImageSource对象
        guard let imageSource = CGImageSourceCreateWithData(data, nil) else { return }
        let imageCount = CGImageSourceGetCount(imageSource)
        
        // 便利所有的图片
        var images = [UIImage]()
        var totalDuration : TimeInterval = 0
        for i in 0..<imageCount {
            // .取出图片
            guard let cgImage = CGImageSourceCreateImageAtIndex(imageSource, i, nil) else { continue }
            let image = UIImage(cgImage: cgImage)
            if i == 0 {
                imageView.image = image
            }
            images.append(image)
            
            // 取出持续的时间
            guard let properties = CGImageSourceCopyPropertiesAtIndex(imageSource, i, nil) else { continue }
            guard let gifDict = (properties as NSDictionary)[kCGImagePropertyGIFDictionary] as? NSDictionary else { continue }
            guard let frameDuration = gifDict[kCGImagePropertyGIFDelayTime] as? NSNumber else { continue }
            totalDuration += frameDuration.doubleValue
        }
        
        // 设置imageView的属性
        imageView.animationImages = images
        imageView.animationDuration = totalDuration
        imageView.animationRepeatCount = 0
        
        // 开始播放
        imageView.startAnimating()
    }
}


3.3 封装GIF播放UIImage分类扩展

  1. 图片路径在沙盒中:
 let filepath =  NSHomeDirectory() + "/Documents/launchGif.gif"
 self.launchImage.kyl_startGifWithFilePath(filePath: filepath)


  1. 图片放在工程中:
self.launchImage.kyl_startGifWithImageName(name: "launchGif.gif")


  1. UIImage 扩展gif类的方法:
extension UIImage {
    //MARK:工程内gif
    public func kyl_startGifWithImageName(name:String){
        guard let path = Bundle.main.path(forResource: name, ofType: "gif") else {
            print("SwiftGif: Source for the image does not exist")
            return
        }
        self.kyl_startGifWithFilePath(filePath: path)
    }
    //MARK:实现动图效果
    public func kyl_startGifWithFilePath(filePath:String) {
        //1.加载GIF图片,并转化为data类型
        guard let data = NSData(contentsOfFile: filePath) else {return}
        //2.从data中读取数据,转换为CGImageSource
        guard let imageSource = CGImageSourceCreateWithData(data, nil) else {return}
        let imageCount = CGImageSourceGetCount(imageSource)
        //3.遍历所有图片
        var images = [UIImage]()
        var totalDuration : TimeInterval = 0
        for i in 0..<imageCount {
            //3.1取出图片
            guard let cgImage = CGImageSourceCreateImageAtIndex(imageSource, i, nil) else {continue}
            let image = UIImage(cgImage: cgImage)
            images.append(image)
            
            //3.2取出持续时间
            guard let properties = CGImageSourceCopyPropertiesAtIndex(imageSource, i, nil) as? NSDictionary  else {continue}
            guard let gifDict = properties[kCGImagePropertyGIFDictionary]  as? NSDictionary else  {continue}
            guard let frameDuration = gifDict[kCGImagePropertyGIFDelayTime] as? NSNumber else {continue}
            totalDuration += frameDuration.doubleValue
            
        }
        
        //4.设置imageview的属性
        self.animationImages = images
        self.animationDuration = totalDuration
        self.animationRepeatCount = 0
        
        //5.开始播放
        self.startAnimating()
        
    }
    
    public func kyl_imageStopAnimating() {
        self.stopAnimating()
    }
}


参考大神博客:
https://www.jianshu.com/p/ea145cac1f5f

你可能感兴趣的:(图片处理)