UIImage+GIF.swift SDWebImage中处理GIF的分类的swift版实现

来源:互联网 发布:算法分析专业 编辑:程序博客网 时间:2024/06/10 17:08

//

//  UIImage+GIF.swift

//  swiftPractise

//

//  Created by 云君 on 2017/6/5.

//  Copyright © 2017 pilgrim. All rights reserved.

//


import Foundation

import UIKit

import ImageIO


extension UIImage {


    classfunc animatedGIFWith(data:Data?) -> UIImage? {

        iflet data = data {

            iflet source = CGImageSourceCreateWithData(dataas CFData,nil) {

                let count = CGImageSourceGetCount(source)

                var animatedImage:UIImage? = nil

                if count <=1 {

                    animatedImage = UIImage(data: data)

                } else {

                    var images: [UIImage] = []

                    var duration:TimeInterval = 0.0

                    for iin 0..<count {

                        let image = CGImageSourceCreateImageAtIndex(source, i,nil)

                        if image ==nil {

                            continue

                        }

                        duration = duration + frameDurationAt(index: i, source: source)

                        images.append(UIImage(cgImage: image!, scale: UIScreen.main.scale, orientation: .up))

                    }

                    if duration ==0 {

                        duration = (1.0 /10.0) * Double(count)

                    }

                    animatedImage = UIImage.animatedImage(with: images, duration: duration)

                }

                return animatedImage

            } else {

                returnnil

            }

        } else {

            returnnil

        }

    }


    classfunc frameDurationAt(index:Int, source: CGImageSource) ->TimeInterval {

        var frameDuration:TimeInterval = 0.1

        iflet cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(source, index,nil) {

            let frameProperties = cfFramePropertiesas NSDictionary

            iflet gifProperties = frameProperties[kCGImagePropertyGIFDictionaryas String]as? NSDictionary {

                iflet delayTimeUnclampedProp = gifProperties[kCGImagePropertyGIFUnclampedDelayTimeas String]as? NSNumber {

                    frameDuration = delayTimeUnclampedProp.doubleValue

                } else {

                    iflet delayTimeProp = gifProperties[kCGImagePropertyGIFDelayTimeas String]as? NSNumber {

                        frameDuration = delayTimeProp.doubleValue

                    }

                }


            }

        }

        // Many annoying ads specify a 0 duration to make an image flash as quickly as possible.

        // We follow Firefox's behavior and use a duration of 100 ms for any frames that specify

        // a duration of <= 10 ms. See <rdar://problem/7689300> and <http://webkit.org/b/36082>

        // for more information.


        if frameDuration <0.011 {

            frameDuration = 0.100

        }

        return frameDuration

    }


    classfunc animatedGIF(name: String) -> UIImage? {

        let scale = UIScreen.main.scale

        if scale >1.0 {

            iflet retinaPath = Bundle.main.path(forResource: name.appending("@2x"), ofType:"gif") {

                var data:Data?

                do {

                    data = try Data(contentsOf: URL(fileURLWithPath: retinaPath))

                } catch {

                    print("path获取图片data失败")

                }

                iflet data = data {

                    iflet image = UIImage.animatedGIFWith(data: data) {

                        return image

                    }

                }

            }


            iflet path = Bundle.main.path(forResource: name, ofType:"gif") {

                var data:Data?

                do {

                    data = try Data(contentsOf: URL(fileURLWithPath: path))

                } catch {

                    print("path获取图片data失败")

                }

                iflet data = data {

                    iflet image = UIImage.animatedGIFWith(data: data) {

                        return image

                    }

                }

            }


            return UIImage(named: name)

        } else {

            iflet path = Bundle.main.path(forResource: name, ofType:"gif") {

                var data:Data?

                do {

                    data = try Data(contentsOf: URL(fileURLWithPath: path))

                } catch {

                    print("path获取图片data失败")

                }

                iflet data = data {

                    iflet image = UIImage.animatedGIFWith(data: data) {

                        return image

                    }

                }

            }


            return UIImage(named: name)

        }

    }


    func animatedImageByScalingAndCroppingTo(size:CGSize) -> UIImage? {

        if __CGSizeEqualToSize(self.size, size) || __CGSizeEqualToSize(size, .zero) {

            returnself

        }


        var scaledSize = size

        var thumbnailPoint = CGPoint.zero


        let widthFactor = size.width /self.size.width

        let heightFactor = size.height /self.size.height

        let scaleFactor = widthFactor > heightFactor ? widthFactor : heightFactor

        scaledSize.width = self.size.width * scaleFactor

        scaledSize.height = self.size.height * scaleFactor


        if widthFactor > heightFactor {

            thumbnailPoint.y = (size.height - scaledSize.height) *0.5

        } elseif widthFactor < heightFactor {

            thumbnailPoint.x = (size.width - scaledSize.width) * 0.5

        }


        var scaledImages: [UIImage] = []

        iflet images = self.images {

            for imagein images {

                UIGraphicsBeginImageContextWithOptions(size, false, 0.0)

                image.draw(in: CGRect(x: thumbnailPoint.x, y: thumbnailPoint.y, width: scaledSize.width, height: scaledSize.height))

                iflet newImage = UIGraphicsGetImageFromCurrentImageContext() {

                    scaledImages.append(newImage)

                }

                UIGraphicsEndImageContext()

            }

            return UIImage.animatedImage(with: scaledImages, duration:self.duration)

        } else {

            returnnil

        }

    }


}


原创粉丝点击