iOS Swift如何在我的项目中使用Gif文件

时间:2017-05-09 12:55:32

标签: ios swift3 uiimage

在我的应用中,我需要使用.gif,我确实搜索过它。每个人都要求使用UIImage.gifImageWithName("funny")UIImage.gifWithName("jeremy")添加.gif文件。但我在那些Type 'UIImage' has no member 'gifWithName'上收到错误。如何解决这些问题以及如何在我的应用中使用.gif

4 个答案:

答案 0 :(得分:6)

https://github.com/bahlo/SwiftGif/tree/master/SwiftGifCommon下载UIImage + Gif.swift文件并输入您的项目..

// An animated UIImage
let Gif = UIImage.gif(name: "jerry")

// A UIImageView with async loading
let imageView = UIImageView()
imageView.loadGif(name: "tom")]

demo

答案 1 :(得分:1)

iOS不会直接支持.gif张图片。您必须使用最新版本的第三方库,例如 SDWebImage 。简化的解决方案是使用Webview。

否则, 您可以使用UIImageView+Extension

答案 2 :(得分:0)

此代码似乎可以帮助您!

@IBOutlet weak var whiteView: UIImageView!

let imagePicker = UIImagePickerController()

override func viewDidLoad()
{
    super.viewDidLoad()

    GifView.loadGif(name: "funny")
    imagePicker.delegate = self
  }

此外,您还必须在源代码中添加用于“ loadGif”的Controller,我认为您可以在线下载。

答案 3 :(得分:-1)

//
//  GifFunctions.swift
//  GifFunctions
//
//  Created by Ambu Sangoli on 11/12/16.
//  Copyright © 2016 Ambu Sangoli. All rights reserved.
//

import UIKit
import ImageIO
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l < r
  case (nil, _?):
    return true
  default:
    return false
  }
}




extension UIImage {

    public class func gifImageWithData(_ data: Data) -> UIImage? {
        guard let source = CGImageSourceCreateWithData(data as CFData, nil) else {
            print("image doesn't exist")
            return nil
        }

        return UIImage.animatedImageWithSource(source)
    }

    public class func gifImageWithURL(_ gifUrl:String) -> UIImage? {
        guard let bundleURL:URL = URL(string: gifUrl)
            else {
                print("image named \"\(gifUrl)\" doesn't exist")
                return nil
        }
        guard let imageData = try? Data(contentsOf: bundleURL) else {
            print("image named \"\(gifUrl)\" into NSData")
            return nil
        }

        return gifImageWithData(imageData)
    }

    public class func gifImageWithName(_ name: String) -> UIImage? {
        guard let bundleURL = Bundle.main
            .url(forResource: name, withExtension: "gif") else {
                print("SwiftGif: This image named \"\(name)\" does not exist")
                return nil
        }
        guard let imageData = try? Data(contentsOf: bundleURL) else {
            print("SwiftGif: Cannot turn image named \"\(name)\" into NSData")
            return nil
        }

        return gifImageWithData(imageData)
    }

    class func delayForImageAtIndex(_ index: Int, source: CGImageSource!) -> Double {
        var delay = 0.1

        let cfProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil)
        let gifProperties: CFDictionary = unsafeBitCast(
            CFDictionaryGetValue(cfProperties,
                Unmanaged.passUnretained(kCGImagePropertyGIFDictionary).toOpaque()),
            to: CFDictionary.self)

        var delayObject: AnyObject = unsafeBitCast(
            CFDictionaryGetValue(gifProperties,
                Unmanaged.passUnretained(kCGImagePropertyGIFUnclampedDelayTime).toOpaque()),
            to: AnyObject.self)
        if delayObject.doubleValue == 0 {
            delayObject = unsafeBitCast(CFDictionaryGetValue(gifProperties,
                Unmanaged.passUnretained(kCGImagePropertyGIFDelayTime).toOpaque()), to: AnyObject.self)
        }

        delay = delayObject as! Double

        if delay < 0.1 {
            delay = 0.1
        }

        return delay
    }

    class func gcdForPair(_ a: Int?, _ b: Int?) -> Int {
        var a = a
        var b = b
        if b == nil || a == nil {
            if b != nil {
                return b!
            } else if a != nil {
                return a!
            } else {
                return 0
            }
        }

        if a < b {
            let c = a
            a = b
            b = c
        }

        var rest: Int
        while true {
            rest = a! % b!

            if rest == 0 {
                return b!
            } else {
                a = b
                b = rest
            }
        }
    }

    class func gcdForArray(_ array: Array<Int>) -> Int {
        if array.isEmpty {
            return 1
        }

        var gcd = array[0]

        for val in array {
            gcd = UIImage.gcdForPair(val, gcd)
        }

        return gcd
    }

    class func animatedImageWithSource(_ source: CGImageSource) -> UIImage? {
        let count = CGImageSourceGetCount(source)
        var images = [CGImage]()
        var delays = [Int]()

        for i in 0..<count {
            if let image = CGImageSourceCreateImageAtIndex(source, i, nil) {
                images.append(image)
            }

            let delaySeconds = UIImage.delayForImageAtIndex(Int(i),
                source: source)
            delays.append(Int(delaySeconds * 1000.0)) // Seconds to ms
        }

        let duration: Int = {
            var sum = 0

            for val: Int in delays {
                sum += val
            }

            return sum
        }()

        let gcd = gcdForArray(delays)
        var frames = [UIImage]()

        var frame: UIImage
        var frameCount: Int
        for i in 0..<count {
            frame = UIImage(cgImage: images[Int(i)])
            frameCount = Int(delays[Int(i)] / gcd)

            for _ in 0..<frameCount {
                frames.append(frame)
            }
        }

        let animation = UIImage.animatedImage(with: frames,
            duration: Double(duration) / 1000.0)

        return animation
    }
}

使用任何文件名示例保存上面的代码示例GifFunctions.Swift将其添加到您的项目并使用如下所示

let gifToplay = UIImage.gifImageWithName("YourGifName")
YourImageView.image = (image: gifToplay)

您也可以使用其他方法。

享受(y)