首页 > 解决方案 > 如何在 Swfit 中使用 UserDefaults 保存和加载 UIimage?

问题描述

func saveImage(图像:UIImage,键:字符串){

           UserDefaults.standard.set(image, forKey: key)

        }

        func getImage(key : String) -> UIImage {

                    let image = UserDefaults.standard.object(forKey: key) as? UIImage
                    return image!
                    
        }

标签: swift

解决方案


UserDefaults 可以存储数据、字符串、数字、日期、数组和字典。如果要存储图片,可以将图片转成base64字符串再存储。


推荐阅读