在 tableView 中加载 .gif
Loading .gif in tableView
我有一个有 3 个视图的应用程序,其中 2 个是 tableViews (lists),在第三个视图上,我尝试通过传递数据(文本和 gif)加载 gif,不同对于列表中的每一项。
我找到了一个播放 gif 的扩展名为 SwiftGif,在普通 viewControllers 中使用时效果很好,但 gif 不是在 tableViews.
之间传递数据时移动
SwiftGif 包含函数:
extension UIImageView {
public func loadGif(name: String) {
DispatchQueue.global().async {
let image = UIImage.gif(name: name)
DispatchQueue.main.async {
self.image = image
}
}
}
我的 ThirdView.swift 文件包含:
import Foundation
import UIKit
import ImageIO
let name = String()
var gifs = UIImage.gif(name: name)
struct IngredientsAndDirections {
var ThirdViewArray = [String]()
var Pic = [gifs]
}
但我不知道如何调用 gif 数组上的 loadGif 函数来使我的 gif 动起来。
有什么建议吗?
有一个库叫做 FLAnimatedImage。您可以在您的代码中使用这个库。
After this you just need to import the class and put your gif image inside your resource folder in your Xcode.
import FLAnimatedImage
Then you need to use the following code to see your gif
animation.
guard let gifImageUrl = Bundle.main.path(forResource: "gifImage", ofType: "gif") else { return }
let gifData = try? Data(contentsOf: URL(fileURLWithPath: gifImageUrl))
let animGIFImage = FLAnimatedImage(animatedGIFData: gifData)
yourImageView.animatedImage = animGIFImage
注:-
Define your imageView class name as FLAnimatedImageView in your
storyboard file and your outlet should be like this @IBOutlet weak var
imageView: FLAnimatedImageView!
如果有人正在寻找解决方案 (Swift 4)。
- 我实现了库 https://github.com/swiftgif/SwiftGif
- 创建了包含名称和图像的数组(变量名称:NSArray = [],变量图像:NSArray = []
- 在 viewDidLoad 中插入了所有数组(名称 = ["", "", ""] 和图像 = ["", "", ""] -> 计数必须相同)
- 创建了 TableViewCell.swift 文件,插入了所有数据
- 通过传递数据一切都很好,gif 甚至在 tableViews 中移动
我有一个有 3 个视图的应用程序,其中 2 个是 tableViews (lists),在第三个视图上,我尝试通过传递数据(文本和 gif)加载 gif,不同对于列表中的每一项。
我找到了一个播放 gif 的扩展名为 SwiftGif,在普通 viewControllers 中使用时效果很好,但 gif 不是在 tableViews.
之间传递数据时移动SwiftGif 包含函数:
extension UIImageView {
public func loadGif(name: String) {
DispatchQueue.global().async {
let image = UIImage.gif(name: name)
DispatchQueue.main.async {
self.image = image
}
}
}
我的 ThirdView.swift 文件包含:
import Foundation
import UIKit
import ImageIO
let name = String()
var gifs = UIImage.gif(name: name)
struct IngredientsAndDirections {
var ThirdViewArray = [String]()
var Pic = [gifs]
}
但我不知道如何调用 gif 数组上的 loadGif 函数来使我的 gif 动起来。
有什么建议吗?
有一个库叫做 FLAnimatedImage。您可以在您的代码中使用这个库。
After this you just need to import the class and put your gif image inside your resource folder in your Xcode.
import FLAnimatedImage
Then you need to use the following code to see your gif animation.
guard let gifImageUrl = Bundle.main.path(forResource: "gifImage", ofType: "gif") else { return }
let gifData = try? Data(contentsOf: URL(fileURLWithPath: gifImageUrl))
let animGIFImage = FLAnimatedImage(animatedGIFData: gifData)
yourImageView.animatedImage = animGIFImage
注:-
Define your imageView class name as FLAnimatedImageView in your storyboard file and your outlet should be like this @IBOutlet weak var imageView: FLAnimatedImageView!
如果有人正在寻找解决方案 (Swift 4)。
- 我实现了库 https://github.com/swiftgif/SwiftGif
- 创建了包含名称和图像的数组(变量名称:NSArray = [],变量图像:NSArray = []
- 在 viewDidLoad 中插入了所有数组(名称 = ["", "", ""] 和图像 = ["", "", ""] -> 计数必须相同)
- 创建了 TableViewCell.swift 文件,插入了所有数据
- 通过传递数据一切都很好,gif 甚至在 tableViews 中移动