I have a function that I pass a JSON list of names of .png files that has to be taxed in the SandBox as you read a URL. As it is taxing the pngs consumed memory is growing .. until there comes a time that if the list of pngs that I am taxing is very large When you have already consumed about 1200MB approximately comes an error such as "IDAT written into file"
The function that saves the JSON list would be this:
class func importAndSaveImages(fromJSON: JSON) {
let entries = fromJSON.arrayValue
debugPrint(fromJSON)
for entry in entries {
let number = entry["data"]["idImages"].string?.components(separatedBy: "\r").count
debugPrint(entry["data"]["idImages"])
for i in 0..<number!{
let name = entry["data"]["idImages"].string?.components(separatedBy: "\r")[i]
APIController.saveImages(name: name!)
}
}
}
The function that saves PNG
class func saveImages(name: String) {
let rootUrl = URL(string: RequestServices.shared.BaseUrl)!.appendingPathComponent(RequestServices.shared.DirectoriImatges).appendingPathComponent("")
let imageName = name
var urlImage = rootUrl.absoluteString + imageName
urlImage = urlImage + ".png"
let image = UIImage.imageFromUrlString(urlString: urlImage)
UIImage.saveImage(image: image, imageName: imageName + ".png")
}
The function to save png in sandbox
class func saveImage (image: UIImage?, imageName: String) -> Void {
let documentsPath = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])
let logsPath = documentsPath.appendingPathComponent(self.pathImages())
let fileManager = FileManager.default
var isDir : ObjCBool = false
if fileManager.fileExists(atPath: logsPath!.path, isDirectory:&isDir) {
if isDir.boolValue {
// file exists and is a directory
} else {
// file exists and is not a directory
}
} else {
// file does not exist
do {
try FileManager.default.createDirectory(atPath: logsPath!.path, withIntermediateDirectories: false, attributes: nil)
} catch let error as NSError {
print("Unable to create directory Images \(error.debugDescription)")
return
}
}
let imagePNG = UIImagePNGRepresentation(image!)
do {
try imagePNG?.write(to:logsPath!.appendingPathComponent(imageName), options: .atomic )
print("\(imageName) saved")
return
} catch let error {
print(error)
return
}
}