GithubHelp home page GithubHelp logo

braincrumbz / swiftygif Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alexiscreuzot/swiftygif

0.0 1.0 0.0 145.63 MB

High performance GIF engine

License: MIT License

Swift 98.61% Ruby 0.77% Objective-C 0.62%

swiftygif's Introduction

Language CocoaPods Compatible Carthage compatible Build Status Pod License

SwiftyGif

High performance & easy to use Gif engine


Features

  • UIImage and UIImageView extension based
  • Remote GIFs with customizable loader
  • Great CPU/Memory performances
  • Control playback
  • Allow control of display quality by using 'levelOfIntegrity'
  • Allow control CPU/memory tradeoff via 'memoryLimit'

Installation

With CocoaPods

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 Cartfile, apply its syntax for GitHub repositories:

github "kirualex/SwiftyGif"

With Swift Package Manager

https://github.com/kirualex/SwiftyGif.git

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 oustide 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)

Performances

A SwiftyGifManager can hold one or several UIImageView using the same memory pool. This allows you to tune the memory limits to you 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 offer various controls on the current UIImageView playing your gif file.

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 wether 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:

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.

swiftygif's People

Contributors

alexiscreuzot avatar neobeppe avatar billypchan avatar noblakit01 avatar grzegorzkrukowski avatar kawolum avatar bryanoltman avatar carlo- avatar bigearsenal avatar scottrhoyt avatar simdani avatar buh avatar andrewsb avatar anilgoktas avatar boherna avatar alak avatar congsun avatar rivera-ernesto avatar giuseppepiscopo avatar jaimeagudo avatar jlnquere avatar kirankunigiri avatar legolasw avatar codebeauty avatar chackle avatar moonylzhang avatar natanrolnik avatar misterbunnyworth avatar ethanzxlee avatar mattcomi avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.