I am trying to store Image URL from Xcode and store image into Firebase Storage and Image URL to Firebase Database. I can get to store my image in Firebase Storage but when i try to save image url to firebase database i am getting this error: InvalidFirebaseData', reason: '(setValue:) Cannot store object of type NSURL at profileUrl. Can only store objects of type NSNumber, NSString, NSDictionary, and NSArray.' Here is my function code:
func uploadImage(_ Image: UIImage?, completion: @escaping ((_ url: URL?) -> ()) ){
let storageRef = Storage.storage().reference().child("name.png")
guard let imgData = myImageView.image?.pngData() else { return }
let metaData = StorageMetadata()
metaData.contentType = "image/png"
storageRef.putData(imgData, metadata: metaData) {(metadata, error) in
if error == nil {
print("success")
storageRef.downloadURL(completion: {(url,error) in
completion(url)
print("URL is \(String(describing: url))")
} )
}else{
print("error in save image")
completion(nil)
}
}
}
func saveImage(profileURL: URL, completion: @escaping ((_ url: URL?) -> ()) ){
let dict = ["Price": self.label.text ?? "", "profileUrl": profileURL.absoluteURL] as [String : Any]
self.ref.child("Value").childByAutoId().setValue(dict)
}
And i am calling it here:
func saveFIRData() {
self.uploadImage(self.myImageView.image) { url in
self.saveImage(profileURL: url!) { success in
if success != nil {
print("SUCCESS")}}}}
My code should get a url of image in Firebase database under child "profileUrl".
As the error says, you can't save an NSURL object, since that is not a valid JSON type. You'll want to store the string version of the URL by calling profileURL.absoluteString
on it.
func saveImage(profileURL: URL, completion: @escaping ((_ url: URL?) -> ()) ){
let dict = ["Price": self.label.text ?? "", "profileUrl": profileURL.absoluteString] as [String : Any]
self.ref.child("Value").childByAutoId().setValue(dict)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.