SwiftyGif

repository·master·Indexed 25 days ago

https://github.com/alexiscreuzot/swiftygif

A high-performance GIF engine for iOS that provides extensions for UIImage and UIImageView. It supports local and remote GIFs, playback control, and memory optimization via levelOfIntegrity and memoryLimit. The library includes a SwiftyGifDelegate for lifecycle events and can be integrated into SwiftUI using UIViewRepresentable.

Tokens
2.7K
Snippets
12
Records
14
Agent score
31%

What's inside SwiftyGif

  1. Optimize performance with Integrity and Memory limits

    master

    SwiftyGif provides parameters to balance CPU usage and memory consumption via SwiftyGifManager.

    Integrity Level

    Lowering the levelOfIntegrity causes the player to skip frames, which reduces both CPU and memory usage. This is useful for displaying many GIF previews simultaneously.

    do {
        let gif = try UIImage(gifName: "MyImage.gif", levelOfIntegrity: 0.5)
    } catch {
        print(error)
    }

    Memory Limit

    You can adjust the memoryLimit to control CPU/Memory usage. Note that there is a tradeoff: limiting memory usage typically increases CPU usage.

    By default, the library uses SwiftyGifManager.defaultManager to manage memory pools across multiple UIImageView instances.

  2. Optimize performance with levelOfIntegrity and memoryLimit

    master

    SwiftyGif provides tools to balance CPU/Memory usage:

    1. Level of Integrity: Setting a lower levelOfIntegrity (e.g., 0.5) allows for frame skipping, which reduces CPU and memory usage. This is useful for previewing many GIFs simultaneously.
    2. Memory Limit: You can tune the memory tradeoff via memoryLimit. A SwiftyGifManager can manage multiple UIImageView instances using a shared memory pool. If no manager is explicitly declared, the SwiftyGifManager.defaultManager is used.
    do {
        let gif = try UIImage(gifName: "MyImage.gif", levelOfIntegrity: 0.5)
    } catch {
        print(error)
    }
  3. Quick Start: Displaying a local GIF

    master

    SwiftyGif extends UIImage and UIImageView.

    Important Note on Project Files: Xcode xcassets does not recognize .gif files as images. Do not place your GIF files inside the assets folder. Instead, create a dedicated gif folder in your project directory to manage them.

    Using Code

    To load a GIF from your project files and display it in a UIImageView:

    import SwiftyGif
    
    do {
        let gif = try UIImage(gifName: "MyImage.gif")
        let imageview = UIImageView(gifImage: gif, loopCount: 3) // Plays 3 times
        imageview.frame = view.bounds
        view.addSubview(imageview)
    } catch {
        print(error)
    }

    Using Nib/Storyboard

    If you are using an @IBOutlet for your UIImageView:

    @IBOutlet var myImageView : UIImageView!
    
    // ...
    
    let gif = try UIImage(gifName: "MyImage.gif")
    self.myImageView.setGifImage(gif, loopCount: -1) // -1 for infinite loop
    import SwiftyGif
    
    do {
        let gif = try UIImage(gifName: "MyImage.gif")
        let imageview = UIImageView(gifImage: gif, loopCount: 3)
        imageview.frame = view.bounds
        view.addSubview(imageview)
    } catch {
        print(error)
    }
  4. Install SwiftyGif

    master

    You can install SwiftyGif using CocoaPods, Carthage, or Swift Package Manager.

    ### With CocoaPods
    ```ruby
    source 'https://github.com/CocoaPods/Specs.git'
    use_frameworks!
    pod 'SwiftyGif'

    With Carthage

    github "kirualex/SwiftyGif"

    With Swift Package Manager

    https://github.com/kirualex/SwiftyGif.git
  5. Install SwiftyGif via CocoaPods, Carthage, or Swift Package Manager

    master

    You can install SwiftyGif using any of the following dependency managers:

    CocoaPods

    Add the following to your Podfile:

    source 'https://github.com/CocoaPods/Specs.git'
    use_frameworks!
    pod 'SwiftyGif'

    Carthage

    Add the following to your Cartfile:

    github "kirualex/SwiftyGif"

    Swift Package Manager

    Use the following repository URL: https://github.com/kirualex/SwiftyGif.git

    pod 'SwiftyGif'
  6. Display a local GIF from Nib or Storyboard

    master

    If you have a UIImageView outlet, use setGifImage(_:loopCount:) to load the GIF. Setting loopCount to -1 will cause the GIF to loop forever.

    @IBOutlet var myImageView : UIImageView!
    ...
    
    let gif = try UIImage(gifName: "MyImage.gif")
    self.myImageView.setGifImage(gif, loopCount: -1) // Will loop forever
  7. Display a remote GIF from a URL

    master

    Use setGifFromURL(_:customLoader:) to load a GIF from a remote URL. You can provide a customLoader (like a UIActivityIndicatorView) to show progress while the GIF is downloading.

    // 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)
  8. Display a local GIF programmatically

    master

    Use UIImage(gifName:) to initialize a GIF image and UIImageView(gifImage:loopCount:) to display it. The loopCount parameter determines how many times the GIF plays (use -1 for infinite looping).

    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)
    }
  9. Use SwiftyGif in SwiftUI

    master

    Since SwiftyGif is built on UIImageView, you must wrap it in a UIViewRepresentable to use it in SwiftUI.

    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)
        }
    }
    
    // Usage:
    AnimatedGifView(url: Binding(get: { myModel.gif.url }, set: { _ in }))
  10. Load remote GIFs from a URL

    master

    You can load a GIF directly from a URL and optionally provide a custom loader (like UIActivityIndicatorView) to show during the download process.

    // Set the URL for the GIF
    let url = URL(string: "...")
    let loader = UIActivityIndicatorView(style: .white)
    cell.gifImageView.setGifFromURL(url, customLoader: loader)
    let url = URL(string: "...")
    let loader = UIActivityIndicatorView(style: .white)
    cell.gifImageView.setGifFromURL(url, customLoader: loader)
  11. Control GIF playback and animation

    master

    You can control the playback of a GIF directly on the UIImageView instance using the following methods:

    self.myImageView.startAnimatingGif()
    self.myImageView.stopAnimatingGif()
    self.myImageView.showFrameAtIndexDelta(delta: Int)
    self.myImageView.showFrameAtIndex(index: Int)
    
    // Utility methods
    self.myImageView.isAnimatingGif() // Returns whether the gif is currently playing
    self.myImageView.gifImage!.framesCount() // Returns number of frames for this gif
  12. Control GIF playback and inspect frames

    master

    Use the following methods on UIImageView to control playback and check the state of the GIF:

    Playback Control:

    • startAnimatingGif(): Starts the animation.
    • stopAnimatingGif(): Stops the animation.
    • showFrameAtIndex(index: Int): Jumps to a specific frame.
    • showFrameAtIndexDelta(delta: Int): Moves the animation by a specific delta.

    Inspection:

    • isAnimatingGif(): Returns true if the GIF is currently playing.
    • gifImage!.framesCount(): Returns the total number of frames in the GIF.