我试图拍摄一张照片并将其存储在我的应用中,并在再次查看该视图控制器时将其重新加载。
我正在使用保存图片
let imageName : String = ProcessInfo.processInfo.globallyUniqueString
let fileManager = FileManager.default
let imagePath = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent(imageName)
let image = self.image.image!
let data = image.pngData()
fileManager.createFile(atPath: imagePath as String, contents: data, attributes: nil)
let pic = Picture(context: appData.moc)
pic.pictureName = imageName
project.mainPicture = pic
然后使用加载
if let projectMain = project.mainPicture, let imageName = projectMain.pictureName {
let fileManager = FileManager.default
let imagePath = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent(imageName)
if fileManager.fileExists(atPath: imagePath){
image.image = UIImage(contentsOfFile: imagePath)
}
}
这是因为它可以保存图像并在我返回视图时显示它,但是当我返回视图时显示它