简体   繁体   中英

Save image file to app documents directory in Swift. Does the url to App document directory change even when deployed on the device?

Below are the two functions which I use in the picker view controller to save the file in the App documents directory. And save the url in the local DB & display the image using UIImage(contentsOfFile: ImagePathString)

Everything works well as until this point however as soon as i re-run the simulator images does not show up. I believe its because the app directory keeps changing every time its run on the simulator. My question if its the same when deployed on the device?

 func getDocumentsDirectory() -> NSString {
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    let documentsDirectory = paths[0]
    return documentsDirectory
}

    //Below function is used in imagePickerController

    if let data = UIImagePNGRepresentation(modifiedImage!) {
        let filename = getDocumentsDirectory().stringByAppendingPathComponent("\(NSDate()).png")
        data.writeToFile(filename, atomically: true)
        pathImage = filename
        print(pathImage)
    }

Yes url to App documents folder may change when the app is deployed on the device. Refer below link Technical Note TN2406 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM