As of now, Xcode xcassets folders do not recognize .gif as images. This means you need to put your .gif outside of the assets. I recommend creating a group gif for instance.
Quick Start
SwiftyGif uses familiar UIImage and UIImageView to display gifs.
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)
AnimatedGifView(url: Binding(get: { myModel.gif.url }, set: { _ in }))
Performances
A SwiftyGifManager 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.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 UIImageView playing your gif file.
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 MyController to act as the delegate:
SwiftyGif
High performance & easy to use Gif engine
Features
Installation
With CocoaPods
With Carthage
Follow the usual Carthage instructions on how to add a framework to an application. When adding SwiftyGif among the frameworks listed in
Cartfile
, apply its syntax for GitHub repositories:With Swift Package Manager
How to Use
Project files
As of now, Xcode
xcassets
folders do not recognize.gif
as images. This means you need to put your.gif
outside of the assets. I recommend creating a groupgif
for instance.Quick Start
SwiftyGif uses familiar
UIImage
andUIImageView
to display gifs.Programmaticaly
Directly from nib/storyboard
Remote GIFs
SwiftUI
Add this
UIViewRepresentable
to your code.Then to use it:
Performances
A
SwiftyGifManager
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 theSwiftyGifManager.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.
Controls
SwiftyGif offers various controls on the current
UIImageView
playing your gif file.To allow easy use of those controls, some utility methods are provided :
Delegate
You can declare a SwiftyGifDelegate to receive updates on the gif lifecycle. For instance, if you want your controller
MyController
to act as the delegate:Then simply add an extension:
Benchmark
Display 1 Image
Display 6 Images
Measured on an iPhone 6S, iOS 9.3.1 and Xcode 7.3.