I am writing an iOS application (in Swift) that includes options for saving images to a safe place. As soon as an image is selected (from the camera or saved images), it is written to a file and the file location is saved to NSUserDefaults. I can re-access the image when I restarted the application after killing it. But when I run the application again from Xcode (after recompiling), the image will be lost from the image path.
Joining save and load functions below
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 jpgImageData = UIImageJPEGRepresentation(image, 1.0)
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)")
return image
}
What could be the reason for this?
source
share