1
爲什麼這不工作?我如何獲得所有功能來運行並執行它的任務?我正在嘗試將2張圖片上傳到我的Firebase存儲中,併爲我的Firebase數據庫(帖子內)中的圖片獲取兩個下載網址。如果它不在那裏,我怎麼能忽略第二個圖像?如何將2張圖片同時上傳到Firebase存儲?
@IBAction func pickImage1(_ sender: Any) {
let image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.photoLibrary
image.allowsEditing = false
selected = 1
self.present(image, animated: true)
}
//Add didFinishPickingMediaWithInfo here
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
if selected == 1 {
myImageView1.image = image
} else {
myImageView2.image = image
}
}
else {
//error
}
self.dismiss(animated: true, completion: nil)
}
@IBAction func pickImage2(_ sender: Any) {
let image2 = UIImagePickerController()
image2.delegate = self
image2.sourceType = UIImagePickerControllerSourceType.photoLibrary
image2.allowsEditing = false
selected = 2
self.present(image2, animated: true)
}
@IBAction func upload(_ sender: Any) {
if let image1 = myImageView1.image {
guard let data = UIImagePNGRepresentation(image1) else { return }
let storageRef = Storage.storage().reference().child("images/\(NSUUID().uuidString)/image.png")
storageRef.putData(data, metadata: nil, completion: { (metadata, error) in
if error != nil {
print("error")
return
}
else {
let downloadURL = metadata?.downloadURL()?.absoluteString
self.ref?.child("Posts").childByAutoId().setValue(["Title": self.titleText.text, "Subtitle": self.subtitleText.text, "Article": self.articleText.text, "Author": self.authorText.text, "Date": self.dateText.text, "Tags": self.tagsText.text, "PostType": self.postType.text, "PostStyle": self.postStyle.text, "PostSize": self.postSize.text, "Download URL": (downloadURL)])
return
}
})
}
if let image2 = myImageView2.image {
guard let data = UIImagePNGRepresentation(image2) else { return }
let storageRef = Storage.storage().reference().child("images/\(NSUUID().uuidString)/image1.png")
storageRef.putData(data, metadata: nil, completion: { (metadata, error) in
if error != nil {
print("error")
return
}
else {
let downloadURL = metadata?.downloadURL()?.absoluteString
let downloadURL2 = metadata?.downloadURL()?.absoluteString
self.ref?.child("Posts").childByAutoId().setValue(["Title": self.titleText.text, "Subtitle": self.subtitleText.text, "Article": self.articleText.text, "Author": self.authorText.text, "Date": self.dateText.text, "Tags": self.tagsText.text, "PostType": self.postType.text, "PostStyle": self.postStyle.text, "PostSize": self.postSize.text, "Download URL": (downloadURL), "Download URL 2": (downloadURL2)])
}
})
}
}
所以我應該把數據庫部分兩次處理它的每個部分? – Riccardo
如果'image'爲零或無法上傳,您是否只想上載'image1'? –
我想一直上傳image1並上傳Image2(如果有的話) – Riccardo