1

Does anyone know how to acquire the absolute path from an image using iOS Swift? I am trying to send the file path in a HTTP post request. I have tried the code below but it takes too long to process. I am trying to send the file path of an image to a PostgreSQL database. Any tips are greatly appreciated. Thank you!

var paths: NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
   var documentDirectory: NSString = paths.objectAtIndex(0) as NSString
   var localFilePath: NSString = documentDirectory.stringByAppendingPathComponent("image.png")
   var data: NSData = UIImagePNGRepresentation(image)
   data.writeToFile(localFilePath, atomically: true)
  • What is `image`? If the question is about the path to an image stored in the photo album, then you can't get its absolute path, compare http://stackoverflow.com/questions/11556100/alassetslibrary-getting-the-path-of-a-video-and-play-it-later or http://stackoverflow.com/questions/12121204/how-can-i-get-original-nsdata-of-uiimage. – Martin R Jan 05 '15 at 07:00

1 Answers1

0
let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first as NSURL
if let fileAbsoluteUrl = documentsUrl.URLByAppendingPathComponent( "image.png").absoluteURL {
    println(fileAbsoluteUrl)
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571