2017-06-29 124 views
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)]) 


      } 

     }) 
    } 

}

回答

4

要检查文件是否已经存在,你想打电话storageRef.metadataWithCompletion

解开一个可选的图像你可以做

if let image = self.myImageView2.image as? UIImage { // upload it }

编辑

像这样的事情

if let image = imageView.image { 
     guard let data = UIImagePNGRepresentation(image) else { return } 

     let storageRef = Storage.storage().reference().child("images/\(NSUUID().uuidString)/image.png") 
     storageRef.putData(data, metadata: nil, completion: { (metadata, error) in 
      // Handle it 
     } 
    } 

    if let image1 = imageView1.image { 
     guard let data = UIImagePNGRepresentation(image1) else { return } 

     let storageRef = Storage.storage().reference().child("images/\(NSUUID().uuidString)/image1.png") 
     storageRef.putData(data, metadata: nil, completion: { (metadata, error) in 
      // Handle it 
     } 
    } 

另外,还要确保你与火力地堡否则上传authticated将失败。检查控制台它会告诉你,如果认证失败。

EDIT2

这应该做的伎俩。

func upload(image: UIImage, completion: @escaping (String?) ->()) { 
    guard let data = UIImagePNGRepresentation(image) else { return completion(nil)} 

    let storageRef = databaseStorage.child("images/\(NSUUID().uuidString)/image.png") 
    storageRef.putData(data, metadata: nil) { (metaData, error) in 
     if error != nil { 
      print(error?.localizedDescription ?? "Failed to upload image") 
     } else if metaData != nil && metaData?.downloadURL() != nil { 
      completion(metaData?.downloadURL()!.absoluteString) 
     } else { 
      completion(nil) 
     } 
    } 
} 

func upload() { 
    if imageView.image == nil && imageView1.image == nil { 
     return; 
    } 

    if let image = imageView.image { 
     upload(image: image, completion: { [weak self] (url) in 
      if let image1 = self?.imageView1.image { 
       self?.upload(image: image1, completion: { (url1) in 
        if url == nil && url1 == nil { 
         return 
        } else if url == nil { 
         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 URL1": (url1)]) 
        } else if url1 == nil { 
         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": (url)]) 
        } else { 
         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": (url), "Download URL1" : (url1)]) 
        } 
       }) 
      } 
     }) 
    } else { 
     if let image1 = self.imageView1.image { 
      self.upload(image: image1, completion: { (url1) in 
       if (url1 == nil) { 
        return 
       } 

       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": (url)]) 

      }) 
     } 
    } 
} 
+0

所以我应该把数据库部分两次处理它的每个部分? – Riccardo

+0

如果'image'为零或无法上传,您是否只想上载'image1'? –

+0

我想一直上传image1并上传Image2(如果有的话) – Riccardo

相关问题