Skip to content

Instantly share code, notes, and snippets.

@westerlund
Created December 22, 2014 17:07
Show Gist options
  • Star 41 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save westerlund/eae8ec71cdac88be7c3a to your computer and use it in GitHub Desktop.
Save westerlund/eae8ec71cdac88be7c3a to your computer and use it in GitHub Desktop.
Create an animated gif in swift
func createGIF(with images: [UIImage], loopCount: Int = 0, frameDelay: Double, callback: (data: NSData?, error: NSError?) -> ()) {
let fileProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: loopCount]]
let frameProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFDelayTime as String: frameDelay]]
let documentsDirectory = NSTemporaryDirectory()
let url = NSURL(fileURLWithPath: documentsDirectory)?.URLByAppendingPathComponent("animated.gif")
if let url = url {
let destination = CGImageDestinationCreateWithURL(url, kUTTypeGIF, UInt(images.count), nil)
CGImageDestinationSetProperties(destination, fileProperties)
for i in 0..<images.count {
CGImageDestinationAddImage(destination, images[i].CGImage, frameProperties)
}
if CGImageDestinationFinalize(destination) {
callback(data: NSData(contentsOfURL: url), error: nil)
} else {
callback(data: nil, error: NSError())
}
} else {
callback(data: nil, error: NSError())
}
}
@CharlesPanel
Copy link

I cannot get this to work...Once I have the data, I create a UIImage to use as the GIF, but it doesn't work, and the UIImage remains frozen on the first image.

@westerlund
Copy link
Author

@ChMaxou while UIImage supports GIFs UIImageView does not. I suggest you check out https://github.com/Flipboard/FLAnimatedImage for displaying GIFs in your app! I can also add that if you save the image to your camera roll, it will still be a GIF. If you send that image using any medium that supports GIFs, such as mail, it'll animate. Good luck!

@Rotemy
Copy link

Rotemy commented Jul 15, 2018

Swift 4.2 (with rx)

private func createPreviewGIF(with images: [UIImage], loopCount: Int = 0, frameDelay: Double) -> Observable<URL?> {
    
    return Observable.create({ subscriber in

        let fileProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: loopCount]] as CFDictionary
        let frameProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFDelayTime as String: frameDelay]] as CFDictionary
        
        let documentsDirectory = NSTemporaryDirectory()
        let url = NSURL(fileURLWithPath: documentsDirectory).appendingPathComponent("\(Utils.randomString(length: 8)).gif")
        
        if let url = url, let destination = CGImageDestinationCreateWithURL(url as CFURL, kUTTypeGIF, images.count, nil) {
            
            CGImageDestinationSetProperties(destination, fileProperties)
            
            for i in 0..<images.count {
                
                if let cgImage = images[i].cgImage {
                    CGImageDestinationAddImage(destination, cgImage, frameProperties)
                }
                
            }
            
            if CGImageDestinationFinalize(destination) {
                
                subscriber.onNext(url)
                
            } else {
                
                subscriber.onNext(nil)

            }
            
        } else  {
         
            subscriber.onNext(nil)

        }
        
        subscriber.onCompleted()
        
        return Disposables.create()

    })
    
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment