在我的應用程序中,我需要使用.gif
,並且我搜索了它。每個人都要求使用UIImage.gifImageWithName("funny")
或UIImage.gifWithName("jeremy")
來添加.gif
文件。但我在Type 'UIImage' has no member 'gifWithName'
上遇到錯誤。如何解決這些問題以及如何在我的應用中使用.gif
。iOS Swift如何在我的項目中使用Gif文件
3
A
回答
3
下載的UIImage + Gif.swift從https://github.com/bahlo/SwiftGif/tree/master/SwiftGifCommon文件,並把你的項目..
// An animated UIImage
let Gif = UIImage.gif(name: "jerry")
// A UIImageView with async loading
let imageView = UIImageView()
imageView.loadGif(name: "tom")]
1
iOS將不直接支持.gif
圖片。您必須使用最新版本的第三方庫,如SDWebImage。簡化的解決方案是使用Webview。
否則, 您可以使用UIImageView+Extension
0
//
// 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)
相關問題
- 1. 如何在我的項目中隱藏gif或mp3文件?
- 2. 在Swift項目中使用C++文件
- 3. 如何在iOS中使用一個項目中的多個UINavgationController使用SWIFT
- 4. 如何從目標c中的pod文件訪問我的swift項目文件?
- 5. 我如何在我自己的iOS項目中使用pjsip?
- 6. 如何在iOS項目中使用dylib庫和hpp頭文件?
- 7. 如何從項目中的GIF文件中獲取圖像
- 8. 當我在IOS項目中使用ARC時,如何使用void *?
- 9. 如何在目標文件中使用swift文件c
- 10. 在離子項目中使用Swift庫
- 11. OpenCV在Swift IOS項目中讀取文件
- 12. 如何在我的IOS項目中使用Mailcore框架?
- 13. 如何在我的HTML項目中使用sass/scss文件?
- 14. 如何在我的ASP.NET項目中使用JavaScript文件?
- 15. 如何在我的aspx文件中使用resourcefile項目?
- 16. 如何在Swift項目上使用BSKeyboardControls?
- 17. 如何使用Swift 3中的Pod使用Swift 4.0構建xcode 9項目?
- 18. 如何在swift-ios文件中使用asl.h
- 19. 我如何通過使用橋文件將我的swift代碼合併到現有的目標c項目中
- 20. Swift:在iOS項目中使用Swift是否需要在lib文件中打包libswiftCore.dylib?
- 21. 如何在ios的啓動畫面中添加.gif文件?
- 22. 使用swift插件來使用swift項目!use_frameworks?
- 23. 如何在C++函數中訪問iOS項目中的文件?
- 24. iOS + Swift,如何訪問目錄中的所有音樂文件
- 25. Swift項目中的Swift莢
- 26. 如何使用我的visual studio項目中包含的文件?
- 27. iOS 8 Swift:我如何在Table View的Detail View中顯示來自項目的.txt文件?
- 28. 可可豆在Swift項目中使用目標c文件。橋接問題
- 29. iOS在另一個項目中使用現有項目的類/資源文件
- 30. 如何在Swift中使用MKSnapshotter(iOS)
你是集成了基類 –
您可以使用庫爲了它。 https://github.com/Flipboard/FLAnimatedImage這個庫可以幫助你在應用中加載gif –
iOS不支持GIF文件,所以你必須使用它的庫。所以,如果你沒有庫,那麼你必須加載圖像數組作爲gif圖像 –