本文介绍了Firebase使用Firebase数据库保存从URL中保存的图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经将图片保存到Firebase存储,并将imageURL保存到Firebase数据库,但是如何通过保存在Firebase数据库中的网址获取图片?这里是我如何保存它
func imagePickerController(picker:UIImagePickerController,didFinishPickingImage image:UIImage,editingInfo:[String:AnyObject]? ){
userPhoto.image = image
dismissViewControllerAnimated(true,completion:nil)
var data = NSData()
data = UIImageJPEGRepresentation(userPhoto.image !, 0.8)!
//设置上传路径
let filePath =\(FIRAuth.auth()!. currentUser!.uid)/ \(userPhoto)
let metaData = FIRStorageMetadata )
metaData.contentType =image / jpg
self.storageRef.child(filePath).putData(data,metadata:metaData){(metaData,error)in $ b $ if if error =错误{
print(error.localizedDescription)
return
} else {
// store downloadURL
let downloadURL = metaData!.downloadURL()!. absoluteString
// store downloadURL at database
self.databaseRef.child(users)。child(FIRAuth.auth()!. currentUser!.uid).updateChildValues([userPhoto:downloadURL])
$ b $ / code $ / pre
解决方案这里有一种方式,通过下载数据,然后为它创建一个 UIImage
:
self.storage.referenceForURL(URL).dataW ithMaxSize(25 * 1024 * 1024,完成:{(data,error) - >无效
让image = UIImage(data:data!)
chatMessage.image = image!
self.messages.append(chatMessage)
self.tableView.reloadData()
self.scrollToBottom()
})
这段代码来自。完整的代码可在
I have saved an image to the Firebase storage and also saved the imageURL to Firebase database, but how can I get the image back by the URL saved in firebase database? Here is how I save it
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
userPhoto.image = image
dismissViewControllerAnimated(true, completion: nil)
var data = NSData()
data = UIImageJPEGRepresentation(userPhoto.image!, 0.8)!
// set upload path
let filePath = "\(FIRAuth.auth()!.currentUser!.uid)/\("userPhoto")"
let metaData = FIRStorageMetadata()
metaData.contentType = "image/jpg"
self.storageRef.child(filePath).putData(data, metadata: metaData){(metaData,error) in
if let error = error {
print(error.localizedDescription)
return
} else {
// store downloadURL
let downloadURL = metaData!.downloadURL()!.absoluteString
// store downloadURL at database
self.databaseRef.child("users").child(FIRAuth.auth()!.currentUser!.uid).updateChildValues(["userPhoto": downloadURL])
}
}
}
解决方案 Here's one way of doing it, by downloading the data and then creating a UIImage
for it:
self.storage.referenceForURL(url).dataWithMaxSize(25 * 1024 * 1024, completion: { (data, error) -> Void in
let image = UIImage(data: data!)
chatMessage.image = image!
self.messages.append(chatMessage)
self.tableView.reloadData()
self.scrollToBottom()
})
This code comes from the Zero To App talk at Google I/O. Complete code is available in this gist
这篇关于Firebase使用Firebase数据库保存从URL中保存的图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!