I'm attempting to grab the file path of an image selected through an imagePickerController in order to upload the file to Firebase Storage.
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
mediaUploadView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
let localUrl: NSURL = (info[UIImagePickerControllerReferenceURL] as! NSURL)
print(localUrl)
let localFile: NSURL = localUrl
let mediaRef = storageRef.child("media")
let uploadTask = mediaRef.putFile(localUrl, metadata: nil) { metadata, error in
if (error != nil) {
} else {
let downloadURL = metadata!.downloadURL
}
}
self.dismissViewControllerAnimated(true, completion: nil)
}
The output reads:
assets-library://asset/asset.JPG?id=C224DA06-6B7B-4BE2-8F5E-2EC7BC8B0526&ext=JPG
Body file is unreachable: /asset.JPG
Error Domain=NSCocoaErrorDomain Code=4 "The file doesn’t exist."
It grabs what I believe is the NSURL, but then is unable to find it for upload.
I have attempted to follow along with this similar problem on stack "How do I get Image reference and upload image to Firebase?" but the output then simply returns nil. Any help would be great, thank you!
I had trouble getting the info[UIImagePickerControllerOriginalImage]
to work. I had to do it like this:
let assets = PHAsset.fetchAssetsWithALAssetURLs([localUrl], options: nil)
let asset = assets.firstObject
asset?.requestContentEditingInputWithOptions(nil, completionHandler: { (contentEditingInput, info) in
let imageFile = contentEditingInput?.fullSizeImageURL
// now call putFile with imageFile instead of localURL
来源:https://stackoverflow.com/questions/37670009/obtain-nsurl-from-uiimagepickercontroller