I'm using the following code to convert images to data to store them in core data, however its causing the app to crash due to running out of memory.
let imagePath = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent(mainPicName)
if fileManager.fileExists(atPath: imagePath) {
DispatchQueue.main.async {
if let image : UIImage = UIImage(contentsOfFile: imagePath) {
let imageData = image.pngData()
}
}
}
If I comment out the let imageData = image.pngData() line then I don't get the issue
With the line commented out, the memory usage once the app has loaded is less than 100MB, if its not commented it can get to 2GB before crashing