I had an application that runs the following code snippets --
...
let newImage = generateImage()
let imageData = newImage.jpegData(compressionQuality: 1)
// operations with the imageData. The memory consumption of the
// imageData object looks reasonable, 2-3 MB for a 4800*6000
// pixel image.
...
func generateImage() -> UIImage {
let canvasSize = self.outputImage.size
UIGraphicsBeginImageContext(canvasSize)
self.imageView.zoomView?.layer.render(in: UIGraphicsGetCurrentContext()!)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
The observation is that at the step of the following codes, I will see a memory spike
let imageData = outputImage.jpegData(compressionQuality: 1)
And this memory spike will not disappear until the whole view is released. I do have tried some autoreleasepool() or other put in DispatchQueue() operation as suggested in here swift UIGraphicsGetImageFromCurrentImageContext can't release memory but apparently they do not work.
To reduce confusion, the generateImage() call does not produce memory leak but just spike, i.e., the memory goes down right after the generateImage() call, but I am just putting it there to see if this related (I tried some other UIImage with the jpegData() but I cannot reproduce this issue 100% though).
I hesitate to call this a memory leak as I believe this object (I actually do not know why the UIImage.jpegData() call consumed that much memory too, looks like a bug with Swift memory management algorithm) is finally recycled when the view is unloaded. But is there any way to avoid the huge memory consumption or release the memory caused by the jpegData conversion?
(The memory will go up ~100MB with this UIImage.jpegData() operation when loading 24MB image on my iPhone with 4800*6000Pixels).