从UIImagePickerController获取NSURL [英] Obtain NSURL from UIImagePickerController

查看:182
本文介绍了从UIImagePickerController获取NSURL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图抓取通过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
mediaRef = storageRef.child(media)

let uploadTask = mediaRef.putFile(localUrl,元数据:nil){元数据,
中的错误if(error!= nil){
} else {
let downloadURL = metadata!.downloadURL
}
}


self.dismissViewControllerAnimated(true,completion:nil)




$ p
$输出结果如下:

  assets-library://asset/asset.JPG?id = C224DA06-6B7B-4BE2-8F5E-2EC7BC8B0526& ext = JPG 
正文文件无法访问: /asset.JPG
错误域= NSCocoaErrorDomain代码= 4该文件不存在。

它抓住了我所认为的NSURL,但是却无法找到上传。 p>

我试图在堆栈中跟随这个类似的问题如何获取图片引用并将图片上传到Firebase?,但输出结果只是返回零。
任何帮助将是伟大的,谢谢!

解决方案

我得到信息[UIImagePickerControllerOriginalImage] 来工作。我必须这样做:

$ p $ lt; code> let assets = PHAsset.fetchAssetsWithALAssetURLs([localUrl],options:nil)
让asset = assets.firstObject
asset。.requestContentEditingInputWithOptions(nil,completionHandler:{(contentEditingInput,info)in
let imageFile = contentEditingInput?.fullSizeImageURL
//现在用imageFile调用putFile localURL


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

这篇关于从UIImagePickerController获取NSURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆