ios swift 本地以及网络获取GIF图片并展示

//
//  XGIFView.swift
//  UITablViewTest
//
//  Created by Marico on 2017/2/14.
//  Copyright © 2017年 Marico. All rights reserved.
//

import UIKit
import ImageIO
import QuartzCore

/**
 * MD5字符转换需要一个OC的库文件,可以创建链接桥添加库文件 #import <CommonCrypto/CommonDigest.h>
 * 网路加载存储的图片路径可以自定义位置以及名称
 */

class XGIFView: UIView {
    var width:CGFloat{return self.frame.size.width}
    var height:CGFloat{return self.frame.size.height}
    private var gifurl:NSURL!
    private var totalTime:Float = 0
    private var imageArray:Array<CGImage> = []
    private var timeArray:Array<NSNumber> = []
    
    /**
     * 本地加载GIF图片
     */
    func showGifImagwWithLocalName(name: String) {
        gifurl = Bundle.main.url(forResource: name, withExtension: "gif") as NSURL!
        self.createFrame()
    }
    
    /**
     * 网络端加载GIF图片
     */
    func showGIFImageFromNetWork(url:NSURL) {
        let fileName = self.getMD5StringFromString(string: url.absoluteString!)
        let filePath = NSHomeDirectory()+"/Library/Caches/" + fileName + ".gif"
        if FileManager.default.fileExists(atPath: filePath) {
            self.gifurl = NSURL(fileURLWithPath: filePath)
            self.createFrame()
        }else{
            let session = URLSession.shared
            let task = session.dataTask(with: url as URL, completionHandler: { (data, response, error) in
                DispatchQueue.main.async {
                    let path = NSURL(fileURLWithPath: filePath)
                    do{
                        try data?.write(to: path as URL)
                        self.gifurl = NSURL(fileURLWithPath: filePath)
                        self.createFrame()
                    }catch{
                        print("写入失败")
                    }
                }
            })
            task.resume()
        }
    }
    
    func createFrame() {
        let url:CFURL = gifurl as CFURL!
        let gifSource = CGImageSourceCreateWithURL(url, nil)
        let imageCount = CGImageSourceGetCount(gifSource!)
        for i in 0..<imageCount {
            let imageRef = CGImageSourceCreateImageAtIndex(gifSource!, i, nil)
            imageArray.append(imageRef!)
            let sourceDict = CGImageSourceCopyPropertiesAtIndex(gifSource!, i, nil) as NSDictionary!
            let imageWidth = sourceDict![String(kCGImagePropertyPixelWidth)] as! NSNumber
            let imageHeight = sourceDict![String(kCGImagePropertyPixelHeight)] as! NSNumber
            if imageWidth.floatValue/imageHeight.floatValue != Float(width/height) {
                self.fitSacle(imageWidth: CGFloat(imageWidth), imageHeight: CGFloat(imageHeight))
            }
            
            let gifDict = sourceDict![String(kCGImagePropertyGIFDictionary)] as! NSDictionary
            let time = gifDict[String(kCGImagePropertyGIFDelayTime)] as! NSNumber
            timeArray.append(time)
            totalTime += time.floatValue
        }
        self.showAnimation()
    }
   
    /**
     *  适应长宽高
     */
    func fitSacle(imageWidth:CGFloat, imageHeight:CGFloat) {
        var newWidth:CGFloat
        var newHeight:CGFloat
        if imageWidth/imageHeight > width/height {
            newHeight = width/(imageWidth/imageHeight)
            newWidth = width
        }else{
            newWidth = height*(imageWidth/imageHeight)
            newHeight = height
        }
        let point:CGPoint = self.center
        self.frame.size = CGSize(width: newWidth, height: newHeight)
        self.center = point
    }
    
    /**
     * 展示GIF
     */
    func showAnimation() {
        let animation = CAKeyframeAnimation(keyPath: "contents")
        var currentTime:Float = 0
        var timeKeys:Array<NSNumber> = []
        
        for time in timeArray {
            timeKeys.append(NSNumber(value: Float(currentTime/totalTime)))
            currentTime += time.floatValue
        }
        animation.keyTimes = timeKeys
        animation.values = imageArray
        animation.duration = TimeInterval(totalTime)
        animation.repeatCount = HUGE
        animation.isRemovedOnCompletion = false
        self.layer.add(animation, forKey: "XGIFView")
    }
    
    /**
     * 从string到MD5
     */
    func getMD5StringFromString(string:String) -> String {
        let str = string.cString(using: .utf8)
        let strlen = CC_LONG(string.lengthOfBytes(using: .utf8))
        let digeTlen = Int(CC_MD5_DIGEST_LENGTH)
        let result = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: digeTlen)
        CC_MD5(str!, strlen, result)
        let hash = NSMutableString()
        for i in 0..<digeTlen {
            hash.appendFormat("%02x", result[i])
        }
        return String(hash as String)
    }
    /**
     * 清理缓存
     */
    func cleanCache() {
        let folderPath = NSHomeDirectory() + "/Library/Caches/"
        do {
            let manger = FileManager.default
            let fileName =  try manger.contentsOfDirectory(atPath: folderPath)
            for name in fileName {
                try manger.removeItem(atPath: folderPath + name)
            }
        } catch {
            dump("清理失败")
        }
    }
}

 

posted @ 2017-02-15 15:01  MaricoSun  阅读(2342)  评论(0编辑  收藏  举报