Swift "IDAT written into file". High memory consumption when saving a one-in-one PNG list

0

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
    }
}
    
asked by Popularfan 10.05.2018 в 17:26
source

1 answer

0

As I see everything you are doing in the main thread, which according to Apple should only be for your UI, the best way to do it is asynchronously with other threads that are not the main thread.

In objectiveC:

dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ { // task in other thread. });

In swift: let myQueue = DispatchQueue (label: "myQueue")

myQueue.async {         // task in other thread.
}

    
answered by 13.05.2018 в 02:09