将图片上传到 Firebase 存储并显示为个人资料图片
Posted
技术标签:
【中文标题】将图片上传到 Firebase 存储并显示为个人资料图片【英文标题】:Upload image to Firebase Storage and show as Profile Image 【发布时间】:2019-10-17 18:12:36 【问题描述】:我在将图片上传到 Firebase 存储方面需要帮助。我的应用程序中有一个配置文件菜单。当用户点击此菜单时,他/她可以看到带有他们的信息和个人资料图片的个人资料。所以我做了它,这样你就可以从图库中选择一张照片。但我需要将照片保存到 Firebase 存储并通过 uid 将 ref 添加到 Firebase 数据库。 另外,用户可能没有照片,所以它会为零,因为数据库中什么都没有。看照片你就明白了
extension ProfileViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
var selectedImage: UIImage?
if let editedImage = info[.editedImage] as? UIImage
selectedImage = editedImage
self.profileImage.image = selectedImage!
self.savedImage = selectedImage
picker.dismiss(animated: true, completion: nil)
else if let originalImage = info[.originalImage] as? UIImage
selectedImage = originalImage
self.profileImage.image = selectedImage!
self.savedImage = selectedImage
picker.dismiss(animated: true, completion: nil)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleSelectProfileImageView) )
profileImage.addGestureRecognizer(tapGesture)
profileImage.isUserInteractionEnabled = true
@objc func handleSelectProfileImageView ()
print("Tapped")
let pickerController = UIImagePickerController()
pickerController.delegate = self
present(pickerController, animated: true, completion: nil)
那么,如何上传和下载图片。 如果用户没有照片(参考数据库)。他将从资产中看到图像。 如果用户有照片,他将看到来自 FB 的图像。 image
【问题讨论】:
您是否查看过 Firebase 文档 Uploading a Picture?还有一个关于如何下载的部分。这很简单。我没有看到您尝试过的任何代码,因此不清楚问题所在。请查看以下指南How to create a Minimal, Complete, and Verifiable example 【参考方案1】:用于上传到 Firebase 存储
let imgData: NSData = NSData(data: UIImageJPEGRepresentation((self.img_Photo?.image)!, 0.5)!)
let _:NSData = NSData(data:UIImagePNGRepresentation(((self.img_Photo?.image)!))!)
self.uploadProfileImageToFirebase(data: imgData)
上传功能
func uploadProfileImageToFirebase(data:NSData)
let randomPic = randomString(length: 10)
let storageRef = Storage.storage().reference().child("Pictures").child("\(value(forKey: "UserUID") ?? randomPic).jpg")
if data != nil
storageRef.putData(data as Data, metadata: nil, completion: (metadata, error) in
if(error != nil)
print(error)
return
guard let userID = Auth.auth().currentUser?.uid else
return
// Fetch the download URL
storageRef.downloadURL url, error in
if let error = error
// Handle any errors
if(error != nil)
print(error)
return
else
// Get the download URL for 'images/stars.jpg'
let urlStr:String = (url?.absoluteString) ?? ""
let values = ["photo_url": urlStr]
self.registerUserIntoDatabaseWithUID(uid: userID, values: values as [String : AnyObject])
)
func registerUserIntoDatabaseWithUID(uid:String, values:[String:AnyObject])
let ref = Database.database().reference(fromURL: "https://domain.firebaseio.com/")
let usersReference = ref.child("users").child((Auth.auth().currentUser?.uid)!)
usersReference.updateChildValues(values) (error, ref) in
if(error != nil)
print(error)
return
self.parentVC?.dismiss(animated: true, completion: nil)
【讨论】:
以上是关于将图片上传到 Firebase 存储并显示为个人资料图片的主要内容,如果未能解决你的问题,请参考以下文章
无法将 facebook 个人资料图片 (Uri) 上传到 Firebase 存储
将 Facebook 图片 URL 上传到 Firebase 存储
将用户上传到 Flutter Web 的图片存储为实际的 .jpg 文件