UserDefaults.standard.set(Image Array)需要10秒以上

时间:2017-10-18 20:45:03

标签: arrays swift image nsuserdefaults nskeyedarchiver

UserDefaults.standard.set(NSKeyedArchiver.archivedData(withRootObject: sortedImage), forKey: "StoredData.homePageImages")需要10秒以上。这有意义吗?有什么建议吗?

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()

打印

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

对他人的建议:(还建议使用UIImageJPEGRepresentation并将其设置为0.5而不是UIImagePNGRepresentation,它可以压缩图像并缩短时间(约不到1秒)) < /强>

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)
}

这个速度稍微快一点,大约5秒

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()

打印

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

这是get函数

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
}

1 个答案:

答案 0 :(得分:1)

当我在1MB JPEG上运行上面的代码时,我得到719个字节的数据,所以我认为它只是一个内存中的指针(在下次运行应用程序时无用)

我认为这不会为您提供可以存储在UserDefaults中的数据。我还建议只使用文件系统(Documents文件夹)来存储这些图像。