【问题标题】:Store UIImagePickerController Image locally in App Assets在 App Assets 中本地存储 UIImagePickerController 图像
【发布时间】:2019-04-12 15:24:08
【问题描述】:

我正在尝试通过 UIImagePickerController 在应用中分配个人资料图片。但是,我需要找到一种存储图像的方法,以便在应用重新启动后它仍然会出现。

我尝试了其他方法,主要是使用 UIImagePNGRepresentation 的方法。但这在 Swift 4+ 中不再存在,所以我需要一些其他方法。

   func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
    {
        picker.dismiss(animated: true)

        profilePicture = info[.editedImage] as! UIImage
    }
}

我想要做的,仍然是将图像显示为个人资料图片。但是当应用程序重新启动时,仍然会出现图像。由于这是来自相机胶卷,我无法将其存储在 .xcassets 中

【问题讨论】:

标签: ios swift uiimage userdefaults


【解决方案1】:

让我们假设这确实是您获取图像的方式:

profilePicture = info[.editedImage] as! UIImage

好的,那一刻你知道图像是什么。因此,将其以已知名称存储到已知位置(例如文档)的磁盘中。在未来的发布中,您所要做的就是查看图像是否存在,如果存在则使用它。

主要是使用 UIImagePNGRepresentation 的那些。但这在 Swift 4+ 中不再存在

是的,确实如此,这就是提取数据并将其存储到磁盘的方法。所发生的只是 name 发生了变化,它现在是一个 UIImage 方法。

https://developer.apple.com/documentation/uikit/uiimage/1624096-pngdata

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    • 2013-02-02
    • 2017-06-03
    • 1970-01-01
    • 2015-11-08
    相关资源
    最近更新 更多