修正Bug日
let bookItemRef = self.ref!.child(values.booktitle.lowercased())
let storageImageRef = Storage.storage().reference().child("BookRentImage").child("\(values.booktitle).jpg")
let uploadTask = storageImageRef.putData(self.ImageData!,metadata:nil){(data,error) in
if error != nil{
print(error.localizedDescription)
return
}
// KVO應用
uploadTask.observe(.success, handler:{(snapshot) in
// 下載剛剛上傳圖片的URL
storageImageRef.downloadRef{(url,error) in
// 要先讓error 被指派為error,才能使用error
if let error = error{
print("失敗",error.localizedDescription)
}else{
// absoluteString完整的網域名稱
if let imageFileURL = url?.absoluteString{
// 確認是否為正確的網址
print("photoImageUploadSuccess:",imageFileURL)
}
// 新增bookImage的url上去,因為如果按下上傳 要傳url要等到上傳結束
bookItemRef.child(values.booktitle).setValue(bookItem.ImagetoDictionary(),withCompletionBlock:{ (error,ref) in
if error = nil{
let alertController = UIAlertControler(title:"Upload Image Success!",message:"Congradulation",preferredStyle: .alert)
let alertAction = UIAlertAction(title:"OK", style:.cancel, handler:nil)
alertController.addAction(alertAction)
self.present(alertControler,animated:true, completion: nil)
}
})
bookItem = Book(booktitle: values.booktitle, bookauthors: values.bookauthors, bookISBN: values.bookISBN, bookimage: self.uploadURL)
// 新增Book的等等資訓
bookItemRef.setValue(bookItem.toDictionary(),withCompletionBlock:{ (error,ref) in
if error == nil {
let alertController = UIAlertController(title: "Upload Success", message: "Congradulation", preferredStyle: .alert)
let alertAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(alertAction)
self.present(alertController, animated: true, completion: nil)
}})
absoluteString
參考網址
使用Firebase Storage、Firebase Realtime Database上傳與下載圖片 Swift5.3,Xcode 12,iOS 14
Download files with Cloud Storage on iOS | Firebase