从UIImagePickerController获取NSURL

时间:2016-06-07 03:29:35

标签: swift firebase swift2 nsurl firebase-realtime-database

我试图抓取通过imagePickerController选择的图片的文件路径,以便将文件上传到Firebase存储。

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)

}

输出显示:

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."

它抓住我认为的NSURL,但后来无法找到它上传。

我试图在堆栈上跟随这个类似的问题" How do I get Image reference and upload image to Firebase?"但输出然后只返回nil。 任何帮助都会很棒,谢谢!

1 个答案:

答案 0 :(得分:2)

我无法让info[UIImagePickerControllerOriginalImage]工作。我不得不这样做:

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