简体   繁体   English

来自UIImagePicker的图像的NSURL

[英]NSURL of an image from UIImagePicker

I am trying to upload an image from my phone to my Amazon S3 bucket: 我正在尝试将手机中的图像上传到我的Amazon S3存储桶:

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 ? 如何从UIImagePickerController获取用户选择的图像的NSURL?

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

First U should save the Image to Document Directory. 首先,U应该将图像保存到文档目录。 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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