GIF exporting last row of pixels incorrectly

I am using CGImageDestinationAddImage in a quick project to create animated GIFs from photos. All this works fine, except that the bottom row of pixels in the final GIF is always the same (these are pixels from the first photo added). Photos are the same size (600x600 pixels).

I noticed that this happens on the iPad 2 running iOS 9.3.5, but it works great on the iPad Pro 12.9 "running iOS 10.2". This may be due to 32-bit or 64-bit, or iOS.

Does anyone know if this is a known issue or what am I doing wrong? I pasted my code below.

 // Load photos var photos = [Data]() for index in 1...5 { if let imageDataURL = Bundle.main.url(forResource: "photo_\(index)", withExtension: "jpg"), let data = try? Data(contentsOf: imageDataURL, options: []) { photos.append(data) } } // Create the GIF let temporaryDirectory = NSTemporaryDirectory() let url = URL(fileURLWithPath: temporaryDirectory).appendingPathComponent("slideshow.gif") guard let destination = CGImageDestinationCreateWithURL(url as CFURL, kUTTypeGIF, photos.count, nil) else { return } // Repeat forever by setting the loop count to 0 let gifFileProperties = [kCGImagePropertyGIFLoopCount as String: 0] let fileProperties = [kCGImagePropertyGIFDictionary as String: gifFileProperties] CGImageDestinationSetProperties(destination, fileProperties as CFDictionary) // Set the frame delay to 2 seconds let gifFrameProperties = [kCGImagePropertyGIFDelayTime as String: 2.0] let frameProperties = [kCGImagePropertyGIFDictionary as String: gifFrameProperties] for photo in photos { if let imageProvider = CGDataProvider(data: photo as CFData), let cgimage = CGImage(jpegDataProviderSource: imageProvider, decode: nil, shouldInterpolate: true, intent: .defaultIntent) { CGImageDestinationAddImage(destination, cgimage, frameProperties as CFDictionary) } } guard CGImageDestinationFinalize(destination) else { return } // Get the gif data. let gifData = try? Data(contentsOf: url) 
+5
source share

Source: https://habr.com/ru/post/1247555/


All Articles