使用UserDefaults保存图像阵列

使用UserDefaults保存图像阵列

本文介绍了使用UserDefaults保存图像阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用,用户在其中拍照,拍照后应将其保存到 UserDefaults 中。我不断收到此错误:

I have an app where the user takes a picture and once the picture is taken it should be saved to UserDefaults. I keep getting this error:

"cannot invoke 'setObject' with an argument list of type '(UIImage. type, forKey:[UIImage])'"

我在这里搜索了文档和帖子,但没有帮助。
这是我的代码。

I've searched through documentation and posts on here, but nothing helps.This is my code.

let defaults = NSUserDefaults.standardUserDefaults()

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {

    picker.dismissViewControllerAnimated(true, completion: nil)

    switch currentClothesSelection!{

        case "1":
            shirts.append(info[UIImagePickerControllerOriginalImage] as! UIImage)

            //this is where the error occurs
            defaults.setObject(UIImage.self, forKey: array1)

        case "2" :
            pants.append(info[UIImagePickerControllerOriginalImage] as! UIImage)
        case "3" :
            shorts.append(info[UIImagePickerControllerOriginalImage] as! UIImage)
        case "4" :
            jackets.append(info[UIImagePickerControllerOriginalImage] as! UIImage)
        case "5" :
            shoes.append(info[UIImagePickerControllerOriginalImage] as! UIImage)

        default:
         break
    }


推荐答案

编辑/更新:

Xcode 10.1•Swift 4.2.1或更高版本

在NSUserDefaults上存储图像不是一个好主意,但是如果您确实想要这样做,则需要将其存储为NSData。

It is not a good idea to store images at NSUserDefaults but if you really want to do it you need to store is as NSData.

我建议将其本地保存到Documents文件夹中的磁盘上。如果您真的想将其保存到NSUserDefaults,我建议仅将其用于小图像,并自行承担风险:)您可以执行以下操作:

I recommend saving it locally to disk at the Documents Folder. If you really want to save it to NSUserDefaults, I recommend using just for small images and do it at your own risk :) You can do as follow:

extension UserDefaults {
    func set(image: UIImage?, quality: CGFloat = 0.5, forKey defaultName: String) {
        guard let image = image else {
            set(nil, forKey: defaultName)
            return
        }
        set(image.jpegData(compressionQuality: quality), forKey: defaultName)
    }
    func image(forKey defaultName:String) -> UIImage? {
        guard
            let data = data(forKey: defaultName),
            let image = UIImage(data: data)
        else  { return nil }
        return image
    }
    func set(images value: [UIImage]?, forKey defaultName: String) throws {
        guard let value = value else {
            removeObject(forKey: defaultName)
            return
        }
        try set(NSKeyedArchiver.archivedData(withRootObject: value, requiringSecureCoding: false), forKey: defaultName)
    }
    func images(forKey defaultName: String) throws -> [UIImage] {
        guard let data = data(forKey: defaultName) else { return [] }

        let object = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data)
        return object as? [UIImage] ?? []
    }
}

测试:

let image = UIImage(data: try! Data(contentsOf: URL(string: "https://i.stack.imgur.com/Xs4RX.jpg")!))!
UserDefaults.standard.set(image: image, forKey: "imageKey")
if let loadedImage = UserDefaults.standard.image(forKey: "imageKey") {
    print(loadedImage.size)  // "(719.0, 808.0)"
}

let images = [image, image]
try? UserDefaults.standard.set(images: images, forKey: "imagesKey")
if let loadedImages = try? UserDefaults.standard.images(forKey: "imagesKey") {
    print(loadedImages.count)  // 2
}

这篇关于使用UserDefaults保存图像阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 21:36