如何获取图片参考并将图片上传到Firebase? [英] How do I get Image reference and upload image to Firebase?

查看:584
本文介绍了如何获取图片参考并将图片上传到Firebase?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS开发新手,我正尝试使用Firebase存储服务来存储我的图像。我在我的应用程序中有以下方法,但我无法找到对UIImage的NSURL引用。

$ p $ @IBAction func getImage(sender:UIButton){
让image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(image,animated:true,completion:nil)
}

func imagePickerController(picker:UIImagePickerController,didFinishPickingMediaWithInfo info:[String:AnyObject]){
let theInfo:NSDictionary = info as NSDictionary
let img:UIImage = theInfo.objectForKey UIImagePickerControllerOriginalImage)as! UIImage
imageView.image = img

让localFile = info [UIImagePickerControllerReferenceURL] as! NSURL
uploadToFireBase(localFile)
self.dismissViewControllerAnimated(true,completion:nil)
}

我使用这个方法尝试并将其上传到Firebase

  func uploadToFireBase(localFile:NSURL) {
let storageRef = FIRStorage.storage()。referenceForURL(gs://logintest-90287.appspot.com/Pictures)
let uploadTask = storageRef.putFile(localFile,metadata:nil){元数据,
中的错误if(error!= nil){
//呃 - 哦,发生了一个错误!
} else {
//元数据包含文件元数据,如大小,内容类型和下载URL。
let downloadURL = metadata!.downloadURL



code


然而,XCode总是告诉我:body file is unreachable:/asset.JPG。我试图通过使用下面的方法来抓住NSURL,但它也不起作用。

  let imageUrl = info [UIImagePickerControllerReferenceURL] as! NSURL 
let imageName = imageUrl.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true).first as String!
let photoURL = NSURL(fileURLWithPath:documentDirectory)
let localPath = photoURL.URLByAppendingPathComponent(imageName!)

有人可以帮忙,告诉我如何将图片上传到Firebase?

解决方案

让localFile = info [UIImagePickerControllerReferenceURL] as! NSURL
let assets = PHAsset.fetchAssetsWithALAssetURLs([localFile],options:nil)
let imageURL = assets.firstObject?.fullSizeImageURL $ b $ uploadToFireBase(imageURL)
info [UIImagePickerControllerReferenceURL]中获得的localFile作为!
> NSURL
不是图片网址,而是图片的资源库网址。然后,您需要使用资产网址获取资源,然后检索到全尺寸图片的网址。



[[Update:1]]



我只能通过请求照片作为可编辑对象来获取路径。不知道为什么。

  let referenceUrl = info [UIImagePickerControllerReferenceURL] as! NSURL 
let assets = PHAsset.fetchAssetsWithALAssetURLs([referenceUrl],options:nil)
let asset = assets.firstObject
asset?.requestContentEditingInputWithOptions(nil,completionHandler:{(contentEditingInput,info)in
让imageFile = contentEditingInput?.fullSizeImageURL

imageFile然后拥有照片的绝对路径该设备。


I am new to iOS development and I am trying to use Firebase Storage service to store my images. I have the following methods in my app, but I am unable to locate the NSURL reference to the UIImage. I use the following methods to grab an image from the library.

@IBAction func getImage(sender: UIButton) {
    let image = UIImagePickerController()
    image.delegate = self
    image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    self.presentViewController(image, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    let theInfo: NSDictionary = info as NSDictionary
    let img: UIImage = theInfo.objectForKey(UIImagePickerControllerOriginalImage) as! UIImage
    imageView.image = img

    let localFile = info[UIImagePickerControllerReferenceURL] as! NSURL
    uploadToFireBase(localFile)
    self.dismissViewControllerAnimated(true, completion: nil)
}

And I use this methods to try and upload it to Firebase

func uploadToFireBase(localFile: NSURL) {
    let storageRef = FIRStorage.storage().referenceForURL("gs://logintest-90287.appspot.com/Pictures")
    let uploadTask = storageRef.putFile(localFile, metadata: nil) { metadata, error in
        if (error != nil) {
            // Uh-oh, an error occurred!
        } else {
            // Metadata contains file metadata such as size, content-type, and download URL.
            let downloadURL = metadata!.downloadURL
        }
    }
}

However, XCode keeps telling me "Body file is unreachable: /asset.JPG". I tried to grab the NSURL by using the following methods but it also does not work.

    let imageUrl          = info[UIImagePickerControllerReferenceURL] as! NSURL
    let imageName         = imageUrl.lastPathComponent
    let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as String!
    let photoURL          = NSURL(fileURLWithPath: documentDirectory)
    let localPath         = photoURL.URLByAppendingPathComponent(imageName!)

Can someone please help and tell me how I can upload the image to Firebase?

解决方案

let localFile = info[UIImagePickerControllerReferenceURL] as! NSURL
let assets = PHAsset.fetchAssetsWithALAssetURLs([localFile], options: nil)
let imageURL = assets.firstObject?.fullSizeImageURL
uploadToFireBase(imageURL)

The localFile you get from info[UIImagePickerControllerReferenceURL] as! NSURL is is not an image url, but an Asset Library URL for the image. You then need to fetch the asset using the asset URL, and retrieve the URL to the full size image.

[[Update: 1]]

I could only get the path to work by going through requesting the photo as an editable object. Not sure why.

let referenceUrl = info[UIImagePickerControllerReferenceURL] as! NSURL
let assets = PHAsset.fetchAssetsWithALAssetURLs([referenceUrl], options: nil)
let asset = assets.firstObject
asset?.requestContentEditingInputWithOptions(nil, completionHandler: { (contentEditingInput, info) in
    let imageFile = contentEditingInput?.fullSizeImageURL

imageFile then has the absolute path to the photo on the device.

这篇关于如何获取图片参考并将图片上传到Firebase?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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