Swift之加载本地或者网络GIF图片

举报
Serendipity·y 发表于 2022/02/16 23:19:33 2022/02/16
【摘要】 // // YDWGIFView.swift // Project // // Created by cptech on 2017/6/19. // Copyright © 2017年 CPTEC...
//
//  YDWGIFView.swift
//  Project
//
//  Created by cptech on 2017/6/19.
//  Copyright © 2017年 CPTECH_ydw. All rights reserved.
//

import UIKit
import ImageIO
import QuartzCore

class YDWGIFView: 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 // GIF动画时间

    private var imageArray:Array<CGImage> = [] // CGImage数组(存放每一帧的图片)
    private var timeArray:Array<NSNumber> = [] // DelayTime数组(存放每一帧的图片的时间)

    // 加载本地GIF图片
    func showGIFImageWithLocalName(name:String) {
        gifUrl = Bundle.main.url(forResource: name, withExtension: "gif")! as NSURL
        self.creatFrame()
    }

    // 加载网络端的GIF图片
    func showGIFImageFromNetWork(url:NSURL) {
        let fileName = self.getMD5StringFromString(string: url.absoluteString!)
        let filePath = NSHomeDirectory()+"/Library/Caches/YDWGIF/" + fileName + ".gif"
        if FileManager.default.fileExists(atPath: filePath) {
            self.gifUrl = NSURL(fileURLWithPath: filePath)
            self.creatFrame()
        } 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.creatFrame()
                    } catch {

                    }
                }
            })
            task.resume()
        }
    }

    // 获取GIF图片的每一帧
    func creatFrame() {
        let url:CFURL = gifUrl as CFURL
        let gifSource  = CGImageSourceCreateWithURL(url, nil) // GIF所有资源
        let imageCount = CGImageSourceGetCount(gifSource!) // GIF图片个数

        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 showAnimation() {
        let animation = CAKeyframeAnimation(keyPath: "contents")
        var current:Float = 0
        var timeKeys:Array<NSNumber> = []

        for time in timeArray {
            timeKeys.append(NSNumber(floatLiteral: Double(current/totalTime)))
            current += time.floatValue
        }

        animation.keyTimes = timeKeys
        animation.values = imageArray
        animation.repeatCount = HUGE;
        animation.duration = TimeInterval(totalTime)
        animation.isRemovedOnCompletion = false
        self.layer.add(animation, forKey: "YDWGIFView")

    }

    // 将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/YDWGIF/"
        let manager = FileManager.default
        do {
            let fileName = try? manager.contentsOfDirectory(atPath: folderPath)
            for name in fileName! {
                try? manager.removeItem(atPath: folderPath+name)
            }
        } catch {

        }
    }

    // 适应长宽比
    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
    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148

以上已封装为一个单独处理gif图片的类,可以直接拖入工程,在相应的控制器当中将需要展示gif图片的UIView继承于该类名,再调用showGIFImageWithLocalName方法传入gif图片名即可(注意:gif图片的后缀名需为gif结尾)。
网络图片的加载只需要调用showGIFImageFromNetWork,传入图片NSURL地址即可。调用cleanCache方法可以清除本地已经缓存的GIF图片。

文章来源: blog.csdn.net,作者:Serendipity·y,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/Forever_wj/article/details/73469110

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。