【发布时间】:2017-01-20 19:02:27
【问题描述】:
我正在编写一个 iOS 应用程序(Swift),其中包括将图片保存到安全位置的选项。一旦选择了一张图片(来自相机或保存的图像),它将被写入文件并且文件位置保存在NSUserDefaults。当我杀死它后重新启动应用程序时,我可以重新访问该图像。但是当我再次从 Xcode 运行应用程序时(重新编译后),图像从图像路径中丢失了。
在下面附加保存和加载功能
func getDocumentsURL() -> NSURL {
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
return documentsURL
}
func fileInDocumentsDirectory(filename: String) -> String {
let fileURL = getDocumentsURL().URLByAppendingPathComponent(filename)
return fileURL.path!
}
func saveImage (image: UIImage, path: String ) -> Bool{
// let pngImageData = UIImagePNGRepresentation(image)
let jpgImageData = UIImageJPEGRepresentation(image, 1.0) // if you want to save as JPEG
let result = jpgImageData!.writeToFile(path, atomically: true)
return result
}
func loadImageFromPath(path: String) -> UIImage? {
let image = UIImage(contentsOfFile: path)
if image == nil {
print("missing image at: \(path)")
}
print("Loading image from path: \(path)") // this is just for you to see the path in case you want to go to the directory, using Finder.
return image
}
这可能是什么原因造成的?
【问题讨论】:
-
函数
saveImage(image: UIImage, path: String )中图片的“路径”是什么
标签: ios swift nsfilemanager