Git Product home page Git Product logo

swiftgif's Introduction

โš ๏ธ UNMAINTAINED โš ๏ธ

This library is no longer maintained. I recommend using Gifu instead.


SwiftGif Swift 3.0 Carthage compatible CocoaPods License MIT Build Status

A small UIImage extension with gif support.

Demo gif

Usage

import SwiftGifOrigin

// An animated UIImage
let jeremyGif = UIImage.gif(name: "jeremy")

// A UIImageView with async loading
let imageView = UIImageView()
imageView.loadGif(name: "jeremy")

// A UIImageView with async loading from asset catalog(from iOS9)
let imageView = UIImageView()
imageView.loadGif(asset: "jeremy")

Installation

CocoaPods

Install CocoaPods with the following command:

gem install cocoapods

Integrate SwiftGif into your Xcode project by creating a Podfile:

platform :ios, '9.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'SwiftGifOrigin', '~> 1.7.0'
end

Run pod install to build your dependencies.

Carthage

Install Carthage with Homebrew using the following command:

brew update
brew install carthage

Add the following line to your Cartfile to add SwiftGif:

github "bahlo/SwiftGif" ~> 1.7.0

Run carthage update to build the framework and drag the built SwiftGif.framework into your Xcode project.

How does it work?

Easy, it does the following:

  1. Find out the duration of every frame
  2. Find the greatest common divisor
  3. Add frames accordingly to the greatest common divisor to an array
  4. Create an animated UIImage with the frames

Testing

$ xcodebuild \
  -project SwiftGif.xcodeproj \
  -scheme SwiftGif \
  -sdk iphonesimulator \
  -destination "platform=iOS Simulator,name=iPhone 8" \
  build test \
  CODE_SIGNING_REQUIRED=NO CODE_SIGN_IDENTITY=""

Inspiration

This project is heavily inspired by uiimage-from-animated-gif. Kudos to @mayoff. ๐Ÿ‘

License

This repository is licensed under the MIT license, more under LICENSE.

swiftgif's People

Contributors

argon avatar bahlo avatar elonpark avatar esqarrouth avatar jakubmazur avatar jazminebarroga avatar joelsimonoff avatar kodelit avatar mackuba avatar mintakson avatar nwittstruck avatar rokgregoric avatar samheather avatar satoshin2071 avatar tanukiti1987 avatar tforzaglia avatar yuhsiang237 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swiftgif's Issues

add support for tvOS in the pod spec

thank you for making this. The code works with no modifications on tvOS however the podfile can't install it due to the spec not offering the tvOS.

Could you add it?
thx!

XCode 8 .gif from .xcassets cannot be loaded

Hello, anyone having any problems loading named .gif? My code is the same as it was before moving to Swift 3.0 and XCode 8 but I can no longer see the .gif playing.

I have updated the SwiftGif to its latest version.

Any ideas?

How to install by pod for swift3 ??

I cannot install SwiftGif for swift3.

My Podfile is ..

pod 'SwiftGifOrigin', '~> 1.6'

But I get the following error.

Analyzing dependencies
[!] Unable to satisfy the following requirements:

- `SwiftGifOrigin (~> 1.6)` required by `Podfile`
- `SwiftGifOrigin (~> 1.6)` required by `Podfile`

None of your spec sources contain a spec satisfying the dependency: `SwiftGifOrigin (~> 1.6)`.

You have either:
 * out-of-date source repos which you can update with `pod repo update`.
 * mistyped the name or version.
 * not added the source repo that hosts the Podspec to your Podfile.

Note: as of CocoaPods 1.0, `pod repo update` does not happen on `pod install` by default.

I run pod repo update, but I get the same error again.

How can I use SwiftGif??

How to pause temporarily the gif?

Hi Bahlo, I've just took a look on your library. Everything works amazingly, but I am in stuck with a problem. How to pause temporarily the gif? I want pause and resume it for some reason (like music play, pause...)

Thanks very much and wait for your reply.

Suggestion, Pause Gif

Do you think it is possible to add a function where you could pause/play the gif at the current frame?

Crash when gif from document directory.

i have download the list of gif from server into my document directory.
now when i m going to display it crash.

mycode:
let imageData = NSData(contentsOfURL: NSURL(fileURLWithPath: dataPath))
let advTimeGif = UIImage.gifWithData(imageData!)

Crash at here::
var delayObject: AnyObject = unsafeBitCast(
CFDictionaryGetValue(gifProperties,
unsafeAddressOf(kCGImagePropertyGIFUnclampedDelayTime)),
AnyObject.self)

Cocoa Pods

Any chance you could add this to Cocoa Pods please, for easy installation into projects?

This image named "nav" dows not exist

I'm using the latest Xcode 9, and when I tryed to run my application I get the error This image named "nav" does not exist, I tryed to use this in Xcode 8 without any problems.

Any idea how to fix this problem?

follow up on #41

Hi, stopAnimating() is not working for me? I use Swift 3 on xcode 8. So basically the stopAnimating call does have no effect and gif continues to loop and loop.

Please see: How to pause temporarily the gif? #41 for context.

Thanks.

Animation Speed

Is there a way to adjust the speed of the animation? Awesome work BTW.

Drop the setup

// jeremy.gif
var url = NSBundle.mainBundle().URLForResource("jeremy", withExtension: "gif")
var imageData = NSData(contentsOfURL: url)

// Returns an animated UIImage
UIImage.animatedImageWithData(imageData)

This uses too many lines to setup the gif. It could have been only:

 UIImage.withGifName("jeremy")

I might do this in the future when I have time

Carthage Does Not Build

adding the following to my Cartfile
github "bahlo/SwiftGif"

results is the following:

*** Skipped building SwiftGif due to the error:
Dependency "SwiftGif" has no shared framework schemes for any of the platforms: iOS

Xcode 8.3 Invalid Redeclaration of Gif

After upgrading to Xcode 8.3, I'm getting a compilation error on the method declaration for gif(_ url: String) that says

Method 'gif' with Objective-C selector 'gif:' conflicts with previous declaration with the same Objective-C selector

And also on gif(_ name: String) I'm getting another error that says

Invalid redeclaration of 'gif'

Seems straightforward since Objective-C doesn't support method overloading and the class is extending the Objective-C class UIImage. Just wondering why this wasn't an issue before the upgrade.

Memory leak issue

I've imported the files and it works just fine with the demo gifs provided but when I load in a 3.9mb 640x400 gif the gif lags and the memory of the device (running in simulator) reaches 223mb, this seems exceedingly high.

Is there a limit for the gif that I'm using that I should take in account?

Errors on Xcode 8 GM

Hey,
There are couple of errors while building on Xcode 8 GM. Could you please fix it and patch an update?

Thanks :)

Memory leak after exiting view controller

Oh, sorry, I just confound Your lib with another :) Anyway I was using Your lib, but it is memory inefficient. Better way is to allocate i.e. 5 frames in memory as cache and prepare another in runtime.

Good luck!

crash when the gif only had single frame

func delayForImageAtIndex line 74:
if delayObject.doubleValue == 0 {
delayObject = unsafeBitCast(CFDictionaryGetValue(gifProperties,
unsafeAddressOf(kCGImagePropertyGIFDelayTime)), AnyObject.self)
}
will cause the issues when there is only single frame

i'am newbie to swift, i add this code to fixed the issues. but i don't know whether this is a good way or not.
if count == 1 {
return UIImage(CGImage: CGImageSourceCreateImageAtIndex(source, 0, nil)!)
}

Crashes with invalid GIF data

Hi, trying to load invalid data as a GIF crashes this code with EXC_BAD_ACCESS. This is due to the unsafe code in delayForImageAtIndex(). Proper checks should be in place and the UIImage.gifWithData() should throw / return nil if invalid data was handed in.

As a quick hack, the following is how I made this 'work' (= not crash):

    class func delayForImageAtIndex(index: Int, source: CGImageSource!) -> Double {
        var delay = 0.1

        guard let cfProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil) as? NSDictionary,
            gifProperties = cfProperties[kCGImagePropertyGIFDictionary as NSString] as? NSDictionary else {
                return delay
        }

        var delayTime = gifProperties[kCGImagePropertyGIFUnclampedDelayTime as NSString]

        if delayTime?.doubleValue == 0.0 {
            delayTime = gifProperties[kCGImagePropertyGIFDelayTime as NSString]
        }

        if let delayTime = delayTime {
            delay = delayTime.doubleValue
        }

        if delay < 0.1 {
            delay = 0.1 // Make sure they're not too fast
        }

        return delay
    }

Also, building for the device, I get the linker error:

ld: framework not found SwiftGifOrigin for architecture arm64
  • Matti

Update to swift3?

Hey,

Just wanted to know if you're planning to update it for Swift3 or if it's abandonned ! So I can look for another third party option ! It would be a shame, it's the more practical and the lightest one !

Gif plays slower than original (30 fps)

I know I'm doing something wrong but don't know what.

I have a gif spinner at 30fps, but it plays about half as slow as the gif.

Does anyone have this issue?

Here is the original gif, but when played on the device it's slower

loader

When played on device:

https://goo.gl/xp9ZBb

Gif url

When it try to open a gif with url (UIImage.gif(url: "www.exampleurl.com/test.gif")) i always get the same message back: Cannot turn image named "www.exampleurl.com/test.gif" into NSData

Does anyone else has the same problem?

Laggy TableView scroll.

Hi,
I created a custom UITableViewCell with UIImageView inside to display the gifs in a TableView, but scrolling the TableView is slow, laggy and get stuck for a few seconds while trying to scroll. What can I do?
Thanks.

Carthage problem

Hello,

When I pull the framework with a carthage update (github "bahlo/SwiftGif" ~> 1.6.1), I can import it normally with "import SwiftGif" but I can't access to any methods of it...

Am I doing wrong or it's a framework issue?
Thank you.

Stop animating when the image is resized.

Hi, the gif is animating properly but when I try to resize the image to fit the screen width, the gif is not animating. Here's the code:

     /* IMAGEVIEW */
    var silvester: UIImage = UIImage.gifWithName("image")!

    let screenW: CGFloat = UIScreen.mainScreen().bounds.size.width
    let imageH: CGFloat = screenW / 1.3
    let sizeRight : CGSize = CGSize(width: screenW, height: imageH)
    silvester = scaleUIImageToSize(silvester, size: sizeRight)

    bgImage = UIImageView(image: silvester)
    self.view.addSubview(bgImage!)

    /* SCROLLVIEW */
    self.scrollView = UIScrollView(frame: view.bounds)
    self.scrollView.backgroundColor = UIColor.blackColor()
    scrollView.contentSize = bgImage.bounds.size
    scrollView.autoresizingMask = UIViewAutoresizing.FlexibleWidth
    scrollView.autoresizingMask = UIViewAutoresizing.FlexibleHeight

    scrollView.addSubview(bgImage)
    view.addSubview(scrollView)

    scrollView.delegate = self

And here is the function I use to scale the UIImage:

    func scaleUIImageToSize(let image: UIImage, let size: CGSize) -> UIImage {
         let hasAlpha = false
         let scale: CGFloat = 0.0 // Automatically use scale factor of main screen

         UIGraphicsBeginImageContextWithOptions(size, !hasAlpha, scale)
         image.drawInRect(CGRect(origin: CGPointZero, size: size))

         let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
         UIGraphicsEndImageContext()

        return scaledImage
   }

What's the problem?
Thanks.

Compile Fail with Xcode 6.3 Final

let cfProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil)
is giving an error =
"Cannot invoke 'CGImageSourceCopyPropertiesAtIndex' with an argument list of type 'CGImageSourceRef,Uint,nil)'

CPU usage spikes when app enters background and then opens again

I noticed that when I add a gif to the scene, the CPU usage remains stable but when I press the home button and reopen the app, the % jumps from around 15% to 50-60%. This even happens on the demo app.

Does this happen for anyone else? If so, any fixes for it?

when i use loadGif error occur

when i use it in viewDidLoad Or viewWillAppear myImg.loadGif(name: "GifImgName") this error occur :
Message from debugger: Terminated due to memory issue

Image not cache

Can my gif save in cache. I load a gif from url in a uiimageview in collectionviewcell. And when I scroll down, then up. I see it has to load again.

Tests fail in Xcode6-Beta5

When running the tests in Xcode6-Beta5, they fail when trying to load the test gif:

let cfImageData = imageData! as CFDataRef

Throws fatal error: unexpectedly found nil while unwrapping an Optional value; this looks to be caused by

imageData = NSData(contentsOfURL: NSBundle.mainBundle().URLForResource("test", withExtension: "gif"))

Returning nil. I've tried various ways of loading images, but can't seem to get the resources to load (UIImage named etc all fail).

Thanks for the awesome library!

gif with storyboard

Hi!
Can i use this framework with storyboard without any code? I want to present gif in LaunchScreen.storyboard file. Thanks!

Gif position transfer to another ViewController

Is it possible to transfer and start animation from current frame in another ViewController? For example if i have i bird flying from a left border and then suddenly i need to transit through segue to another view controller may i have the same position of the bird on another without it being reset to the beginning of animation?

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.