I am trying to combine multiple UIImage as 1 gif animated image. I have a problem if the number of frames (UIImages) is more than 20. The app is closing because of memory leaks. When createGIF starts to make the gif, the memory jumps from 200MB to 2GB and keeps increasing until the app crashes due to the memory leak.
Here is my code:
func createGIF(from images: [UIImage], loopCount: Int = 0, frameDuration: TimeInterval = 0.1, completion: @escaping (URL?) -> Void) {
guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
print("Error: Unable to create documentsDirectory.")
completion(nil)
return
}
let gifURL = documentsDirectory.appendingPathComponent("\(UUID().uuidString).gif")
guard let destination = CGImageDestinationCreateWithURL(gifURL as CFURL, kUTTypeGIF, images.count, nil) else {
print("Error: Unable to create CGImageDestination.")
completion(nil)
return
}
let gifProperties = [
kCGImagePropertyGIFDictionary as String: [
kCGImagePropertyGIFLoopCount as String: loopCount
]
]
CGImageDestinationSetProperties(destination, gifProperties as CFDictionary)
// Process frames in batches to reduce memory usage
let batchSize = 10
let totalFrames = images.count
for startIndex in stride(from: 0, to: totalFrames, by: batchSize) {
let endIndex = min(startIndex + batchSize, totalFrames)
let batchImages = Array(images[startIndex..<endIndex])
for image in batchImages {
if let cgImage = image.cgImage {
let frameProperties = [
kCGImagePropertyGIFDictionary as String: [
kCGImagePropertyGIFDelayTime as String: frameDuration
]
]
CGImageDestinationAddImage(destination, cgImage, frameProperties as CFDictionary)
} else {
print("Error: Unable to retrieve CGImage from UIImage.")
completion(nil)
return
}
}
}
guard CGImageDestinationFinalize(destination) else {
print("Error: Failed to finalize CGImageDestination.")
completion(nil)
return
}
completion(gifURL)
}
I am trying to achieve a sticker animation gif from UIImage array. What did I forget here or what is the problem that makes the memory increase to over 2GB when I use more than 20 FPS.

Well, using
DispatchGroupand resizing the images and converting them to CGImage before generating the actual gif image fixed my problem , the memory max peek between 150 and 200mb on generating gif .here is the final code could help someone :
this is the resize extension :