我正在开展一个Ionic项目,我想在其中上传照片并将其添加到Firebase存储中的特定节点。现在我要做的就是在STORAGE中上传照片 相反,我想要的是将这些照片上传到存储中,但是在节点中引用它们,如下所示:
我已经通过在imgur上传照片并在那里复制链接来手动完成,但我需要一个自动程序让用户将照片上传到特定的城市。
让我们说用户在一个城市,拍照并将其上传到Firebase。通过在STORAGE上传它,可以获取他所在的城市名称,并通过调用它来添加新照片,例如" UserName_CityName_Photo"。但是,由于用户可以在不同时间上传同一城市的多张照片,如何轻松上传新照片而不会覆盖已存储的照片?
出于这个原因,我想知道是否有一种方法可以将照片上传到存储上,而不是直接上传到数据库上,例如通过生成引用URL,以便每个新照片都简单地附加在相应的节点下。
答案 0 :(得分:0)
因此,您可以将图像上传到Firebase数据库,但它必须是二进制或奇怪的东西,而且通常不会接受这种做法。要使这些网址在存储空间中独一无二,您可以将其上传到CityName - > autoIdKey将是唯一的。这也意味着一个城市的所有图像都将以其存储名称存储,以后可能会很方便。
您可以使用this code
获取当前用户所在城市import Foundation
import MapKit
typealias JSONDictionary = [String:Any]
class LocationServices {
let shared = LocationServices()
let locManager = CLLocationManager()
var currentLocation: CLLocation!
let authStatus = CLLocationManager.authorizationStatus()
let inUse = CLAuthorizationStatus.authorizedWhenInUse
let always = CLAuthorizationStatus.authorizedAlways
func getAdress(completion: @escaping (_ address: JSONDictionary?, _ error: Error?) -> ()) {
self.locManager.requestWhenInUseAuthorization()
if self.authStatus == inUse || self.authStatus == always {
self.currentLocation = locManager.location
let geoCoder = CLGeocoder()
geoCoder.reverseGeocodeLocation(self.currentLocation) { placemarks, error in
if let e = error {
completion(nil, e)
} else {
let placeArray = placemarks as? [CLPlacemark]
var placeMark: CLPlacemark!
placeMark = placeArray?[0]
guard let address = placeMark.addressDictionary as? JSONDictionary else {
return
}
completion(address, nil)
}
}
}
}
}
获取城市名称的功能:
func getCityName() -> String {
LocationServices.shared.getAdress { address, error in
if let a = address, let city = a["City"] as? String {
return city
}
else {
return "NO_CITY_NAME"
}
}
}
上传并获取存储网址:
@IBAction func postPressed(_ sender: Any) {
// get the cityname
let city = getCityName()
let uid = Auth.auth().currentUser!.uid
let ref = Database.database().reference()
let storage = Storage.storage().reference(forURL: "gs://YourUrlHere")
let key = ref.child("pois").childByAutoId().key // unique key
let imageRef = storage.child("pois").child(city).child("\(key).jpg")
let data = UIImageJPEGRepresentation(photo, 0.6)
let uploadTask = imageRef.putData(data!, metadata: nil) { (metadata, error) in
if error != nil {
// do something
return
}
imageRef.downloadURL(completion: {(url, error) in
// upload of image worked now get the url to upload to your database
if let url = url, let author = Auth.auth().currentUser?.displayName {
let feed = ["city": ,
"pathToImage": url.absoluteString, // here is your storage url to upload to Firebase DB
: key] as [String: Any]
let postFeed = ["\(key)" : feed]
ref.child("pois").updateChildValues(postFeed, withCompletionBlock: { (error, success) in
if error != nil {
// do something
return
}
else {
// data successfully uploaded
}
})
}
})
}
uploadTask.resume()
uploadTask.observe(.success) { (snapshot) in
// do something when upload is finished
return
}
uploadTask.observe(.failure) { (snapshot) in
// do something on failure
return
}
}