高性能且易于使用的 GIF 引擎
斯威夫特吉夫
高性能和易于使用的GIF引擎
语言切换: 한국어
特征
- 基于 UIImage 和 UIImageView 扩展
- 带有可自定义加载器的远程 GIF
- 出色的 CPU/内存性能
- 控制播放
- 允许使用“完整性级别”控制显示质量
- 允许通过“内存限制”进行控制 CPU/内存权衡
安装
与可可豆荚
source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!
pod 'SwiftyGif'
With Carthage
Follow the usual Carthage instructions on how to add a framework to an application. When adding SwiftyGif among the frameworks listed in , apply its syntax for GitHub repositories:Cartfile
github "kirualex/SwiftyGif"
With Swift Package Manager
https://github.com/kirualex/SwiftyGif.git
How to Use
Project files
As of now, Xcode folders do not recognize as images. This means you need to put your outside of the assets. I recommend creating a group for instance.xcassets
.gif
.gif
gif
Quick Start
SwiftyGif uses familiar and to display gifs.UIImage
UIImageView
Programmaticaly
import SwiftyGif
do {
let gif = try UIImage(gifName: "MyImage.gif")
let imageview = UIImageView(gifImage: gif, loopCount: 3) // Will loop 3 times
imageview.frame = view.bounds
view.addSubview(imageview)
} catch {
print(error)
}
Directly from nib/storyboard
@IBOutlet var myImageView : UIImageView!
...
let gif = try UIImage(gifName: "MyImage.gif")
self.myImageView.setGifImage(gif, loopCount: -1) // Will loop forever
Remote GIFs
// You can also set it with an URL pointing to your gif
let url = URL(string: "...")
let loader = UIActivityIndicatorView(style: .white)
cell.gifImageView.setGifFromURL(url, customLoader: loader)
SwiftUI
Add this to your code.UIViewRepresentable
struct AnimatedGifView: UIViewRepresentable {
@Binding var url: URL
func makeUIView(context: Context) -> UIImageView {
let imageView = UIImageView(gifURL: self.url)
imageView.contentMode = .scaleAspectFit
return imageView
}
func updateUIView(_ uiView: UIImageView, context: Context) {
uiView.setGifFromURL(self.url)
}
}
Then to use it:
AnimatedGifView(url: Binding(get: { myModel.gif.url }, set: { _ in }))
Performances
A can hold one or several UIImageView using the same memory pool. This allows you to tune the memory limits to your convenience. If no manager is declared, SwiftyGif will just use the .SwiftyGifManager
SwiftyGifManager.defaultManager
Level of integrity
Setting a lower level of integrity will allow for frame skipping, lowering both CPU and memory usage. This can be a good option if you need to preview a lot of gifs at the same time.
do {
let gif = try UIImage(gifName: "MyImage.gif", levelOfIntegrity:0.5)
} catch {
print(error)
}
Controls
SwiftyGif offers various controls on the current playing your gif file.UIImageView
self.myImageView.startAnimatingGif()
self.myImageView.stopAnimatingGif()
self.myImageView.showFrameAtIndexDelta(delta: Int)
self.myImageView.showFrameAtIndex(index: Int)
To allow easy use of those controls, some utility methods are provided :
self.myImageView.isAnimatingGif() // Returns whether the gif is currently playing
self.myImageView.gifImage!.framesCount() // Returns number of frames for this gif
Delegate
You can declare a SwiftyGifDelegate to receive updates on the gif lifecycle.
For instance, if you want your controller to act as the delegate:MyController
override func viewDidLoad() {
super.viewDidLoad()
self.imageView.delegate = self
}
Then simply add an extension:
extension MyController : SwiftyGifDelegate {
func gifURLDidFinish(sender: UIImageView) {
print("gifURLDidFinish")
}
func gifURLDidFail(sender: UIImageView) {
print("gifURLDidFail")
}
func gifDidStart(sender: UIImageView) {
print("gifDidStart")
}
func gifDidLoop(sender: UIImageView) {
print("gifDidLoop")
}
func gifDidStop(sender: UIImageView) {
print("gifDidStop")
}
}
Benchmark
Display 1 Image
CPU Usage(average) | Memory Usage(average) | |
---|---|---|
FLAnimatedImage | 35% | 9,5Mb |
SwiftyGif | 2% | 18,4Mb |
SwiftyGif(memoryLimit:10) | 34% | 9,5Mb |
Display 6 Images
CPU Usage(average) | Memory Usage(average) | |
---|---|---|
FLAnimatedImage | 65% | 25,1Mb |
SwiftyGif | 22% | 105Mb |
SwiftyGif(memoryLimit:20) | 45% | 26Mb |
Measured on an iPhone 6S, iOS 9.3.1 and Xcode 7.3.