问题
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)
回答1:
let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first as NSURL
if let fileAbsoluteUrl = documentsUrl.URLByAppendingPathComponent( "image.png").absoluteURL {
println(fileAbsoluteUrl)
}
来源:https://stackoverflow.com/questions/27774401/acquiring-absolute-image-file-path-using-ios-swift