I want to make GIF from video frames. Per frame (UIImage) size is at most 960 * 540.
As GIF is bound to 256 colors, I got more colors issues. frames are poorly generated.
After so many research, I've found Dithering approach that may reduce color bandings.
I didn't go with this approach because default time complexity of dithering is O(N^2) where N is the multiplication of Image's width and height. I don't know how to reduce color banding efficiently without dithering for 200 frames. Is there any other technique or filter available which can reduce color banding efficiently?
Original Video : Original video
The GIF I produced : my GIF
The GIF I need : better-GIF
My Approach: First I saved all frames as UIImage from Video. then I created GIF using following code :
func generateImagesToGif(photos: [UIImage], filename: String) -> Bool {
let documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let path = documentsDirectoryPath.appending(filename)
let fileProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: 0]]
let gifProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFDelayTime as String: 0.5]]
let cfURL = URL(fileURLWithPath: path) as CFURL
if let destination = CGImageDestinationCreateWithURL(cfURL, kUTTypeGIF, photos.count, nil) {
CGImageDestinationSetProperties(destination, fileProperties as CFDictionary?)
for photo in photos {
CGImageDestinationAddImage(destination, photo.cgImage!, gifProperties as CFDictionary?)
}
return CGImageDestinationFinalize(destination)
}
return false
}
Update : Have a look this (High Quality GIF). Their GIF looks like original video. Very low color bandings!