Search code examples
arraysswiftimagensuserdefaultsnskeyedarchiver

UserDefaults.standard.set(Image Array) takes more than 10 second


UserDefaults.standard.set(NSKeyedArchiver.archivedData(withRootObject: sortedImage), forKey: "StoredData.homePageImages") takes more than 10 seconds. Does this make sense? Any suggestions?

func printTime() {
    let date = Date()
    let calendar = Calendar.current
    let hour = calendar.component(.hour, from: date)
    let minutes = calendar.component(.minute, from: date)
    let seconds = calendar.component(.second, from: date)
    print("HERE: hours = \(hour):\(minutes):\(seconds)")
}
let sortedImage = [UIImage]()
... append images to sortedImage (4 images)
printTime()
UserDefaults.standard.set(NSKeyedArchiver.archivedData(withRootObject: sortedImage), forKey: "StoredData.homePageImages")
printTime()

Print

HERE: hours = 4:54:11
HERE: hours = 4:54:23

Suggestion for others: (Also recommend to use UIImageJPEGRepresentation and set it to 0.5 instead of UIImagePNGRepresentation which can compress the images and reduce the time (approx. less than 1 sec))

func saveImages(images: [UIImage]) -> [String] {
    func saveImage(image: UIImage) -> String {
        let imageData = NSData(data: UIImagePNGRepresentation(image)!)
        let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory,  FileManager.SearchPathDomainMask.userDomainMask, true)
        let docs = paths[0] as NSString
        let uuid = NSUUID().uuidString + ".png"
        let fullPath = docs.appendingPathComponent(uuid)
        _ = imageData.write(toFile: fullPath, atomically: true)
        return uuid
    }
    var uuids = [String]()
    for image in images {
        uuids.append(saveImage(image: image))
    }
    print("saveImages:", uuids)
    return uuids
}
func removeImages(uuids: [String]) {
    func removeImage(uuid: String) {
        let fileManager = FileManager.default
        let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
        let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
        let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
        guard let dirPath = paths.first else { return }
        let filePath = "\(dirPath)/\(uuid)"
        do {
            try fileManager.removeItem(atPath: filePath)
        } catch let error as NSError {
            print(error.debugDescription)
        }
    }
    for uuid in uuids {
        removeImage(uuid: uuid)
    }
    print("removeImages:", uuids)
}

This one runs slightly faster like around 5 seconds

printTime()
if let uuids = UserDefaults.standard.stringArray(forKey: "StoredData.homePageImage") {
    removeImages(uuids: uuids)
}
let uuids = saveImages(images: sortedImage)
UserDefaults.standard.set(uuids, forKey: "StoredData.homePageImage")
printTime()

Print

HERE: hours = 5:59:38
removeImages: ["32231CF0-9194-4292-999F-71D7DC45D5DB.png", "39664081-0E81-458C-8E9E-2F3E907EAA2D.png", "B9EE3277-B170-43EC-B45A-EEE068B99D7B.png", "79E42843-8E67-49AA-AACC-50904D93011E.png"]
saveImages: ["C08EF14D-06D0-4456-842A-531FBC3B6678.png", "6666A3C3-949E-490E-97A9-19A5C942FB14.png", "623EFCF0-E38C-40FC-B7A5-32243D7914D0.png", "DA8DD110-5EF3-4CC4-99CA-455D8ECEFC62.png"]
HERE: hours = 5:59:43

Here is the get function

func getImages(uuids: [String]) -> [UIImage] {
    func getImage(uuid: String) -> UIImage? {
        let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
        let nsUserDomainMask    = FileManager.SearchPathDomainMask.userDomainMask
        let paths               = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
        if let dirPath          = paths.first {
            let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent(uuid)
            return UIImage(contentsOfFile: imageURL.path)
        }
        return nil
    }
    var images = [UIImage]()
    for uuid in uuids {
        if let image = getImage(uuid: uuid) {
            images.append(image)
        }
    }
    return images
}

Solution

  • When I run the above code on a 1MB JPEG, I get 719 bytes of Data, so I think it's just an in-memory pointer (which will be useless on the next run of the app)

    I don't think that that gives you data that you can store in UserDefaults. I would also suggest just using the file system (Documents folder) to store these images.