Reputation: 9499
I am trying to upload an image from my phone to my Amazon S3
bucket:
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!)
{
var uploadReq = AWSS3TransferManagerUploadRequest()
uploadReq.bucket = "bucketname";
uploadReq.key = "testimage.png"
uploadReq.body = image; //This line needs an NSURL
}
How can I get the NSURL of the image the user selected from a UIImagePickerController
?
Upvotes: 0
Views: 237
Reputation: 9499
While Zhengjie's answer might work. I found a more simple way to go about it:
let fileURL = NSURL(fileURLWithPath: NSTemporaryDirectory().stringByAppendingPathComponent("imagetoupload"))
let data = UIImagePNGRepresentation(image)
data.writeToURL(fileURL!, atomically: true)
uploadReq.body = fileURL
Upvotes: 1
Reputation: 451
First U should save the Image to Document Directory. then Post the URL. U can do like this :
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
let data = UIImagePNGRepresentation(image) // get the data
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true) as! [String]
let path = paths[0]
let fileName = "testimage.png"
let filePath = path.stringByAppendingPathComponent(fileName) // get the file url to save
var error:NSError?
if !data.writeToFile(photoPath, options: .DataWritingAtomic, error: &error) {// save the data
logger.error("Error write file: \(error)")
}
upload.body = NSURL(string: filePath) // used url to post to server
}
Upvotes: 0