如何在 swift 5 中将 UIImage 保存到 UserDefaults

how to save UIImage to UserDefaults in swift 5

我正在尝试将用户图像保存到 userDefaults,但我总是收到错误消息“线程 1:“尝试为密钥 savedimage 插入非 属性 列表对象(函数)”这是我的代码

import UIKit

class productSelectionPage: UIViewController {
    @IBOutlet weak var productImageView: UIImageView!


    override func viewDidLoad() {
        super.viewDidLoad()

        // Save image
        let image = UIImage(named: "user")
        let imageData = UIImage.jpegData(image)
        UserDefaults.standard.setValue(imageData, forKey: "savedImage")

    }
    @IBAction func offerButtonTapped(_ sender: UIButton) {
        // present image
        let imageData = UserDefaults.standard.object(forKey: "savedImage")
        let image = UIImage(data: (imageData as! NSData) as Data)
        productImageView.image = image
        // crash

        
        
    }
    

}

尝试像这样使用 PropertyListEncoder() 和解码器:

func saveImage() {
    guard let data = UIImage(named: "image").jpegData(compressionQuality: 0.5) else { return }
    let encoded = try! PropertyListEncoder().encode(data)
    UserDefaults.standard.set(encoded, forKey: "KEY")
}

func loadImage() {
     guard let data = UserDefaults.standard.data(forKey: "KEY") else { return }
     let decoded = try! PropertyListDecoder().decode(Data.self, from: data)
     let image = UIImage(data: decoded)
}

如果你曾经使用过 firebase,你就会明白所有的图像和视频都保存为数据,而不是像素。

保存到 UserDefaults 时采用类似的方法

// storing the image data

private func store(image: UIImage, forKey key: String, withStorageType storageType: StorageType) {

    if let pngRepresentation = image.pngData() {
        
            UserDefaults.standard.set(pngRepresentation, forKey: key)
        
    }
}

// retrieving the image data and getting an image

To retrieve it, we will take that image data

    private func retrieveImage(forKey key: String, inStorageType storageType: StorageType) -> UIImage? {
    
        if let imageData = UserDefaults.standard.object(forKey: key) as? Data, 
            let image = UIImage(data: imageData) {
            
            return image
        }
    }
}

您可以简单地将图像转换为数据,如 extension 所示 UIImageView,如下面的代码

//MARK: -|||-To Store Image in Userdefault You can just Simply create an uiimageview extension-||| -
extension UIImageView{
    func saveImage(){
    guard let image = self.image, let data = image.jpegData (compressionQuality: 0.5) else { return }
      let encoded = try! PropertyListEncoder ().encode (data)
      UserDefaults.standard.set(encoded, forKey: "image")
    }
    func loadImage() {
        guard let data = UserDefaults.standard.data(forKey: "image") else { return }
        let decoded = try! PropertyListDecoder().decode(Data.self, from: data)
        let image = UIImage(data: decoded)
        self.image = image
    }
}

这里是如何使用这个 extension:

class ViewController: UViewController {
    @IBOutlet var sendImg: UIImageView!
    @IBOutlet var receiveImg: UlImageView!

    override func viewDidLoad(){
        super.viewDidLoad() {
        sendImg.saveImage()
    }

    @IBAction func loadImageFromUserDefault(_ sender: UIButton) {
        receiveImg.loadImage()
    } 
}

将图像保存在 UserDefault:

yourImage.saveImage()

UserDefault获取图像:

yourImage.loadImage()