【问题标题】:When i press button if image spesific image i wanna alert user, how can i do that? and i dont wanna save data to Firebase if image have specific name当我按下按钮,如果图像特定图像我想提醒用户,我该怎么做?如果图像具有特定名称,我不想将数据保存到 Firebase
【发布时间】:2022-10-18 01:28:17
【问题描述】:

如果我的资产中名为“x”的图像被定义到我的imageView中,我想在单击上传按钮时提醒屏幕,但是如果在图像视图。它仍将数据保存到 Firebase 数据库。请帮帮我。

    @IBAction func uploadButtonClicked(_ sender: Any) {
    if  self.imageView.image == UIImage(named: "x") && self.commentText.text! == "" {
        self.makeAlert(tittleInput: "Error", messageInput: "Please choose photo!")
    }else {
        
        let storage = Storage.storage()
        let storageReference = storage.reference()
        
        let mediaFolder = storageReference.child("media")
        
        if let data = imageView.image?.jpegData(compressionQuality: 0.5) {
            
            let uuid = UUID().uuidString
            
            let imageReference = mediaFolder.child("\(uuid).jpg")
            imageReference.putData(data, metadata: nil) { storageMetaData, error in
                if error != nil {
                    self.makeAlert(tittleInput: "Error", messageInput: error?.localizedDescription ?? "Error")
                } else {
                    imageReference.downloadURL { url, error in
                        if error == nil {
                            let imageURL = url?.absoluteString
                        
                            let fireStoreDatabase = Firestore.firestore()
                            
                            var fireStoreReference : DocumentReference? = nil
                            
                            let fireStorePost = ["imageUrl" : imageURL!, "postedBy": Auth.auth().currentUser?.email! , "postComment": self.commentText.text!,"date": FieldValue.serverTimestamp(), "like": 0] as [String: Any]
                           
                            fireStoreReference = fireStoreDatabase.collection("Posts").addDocument(data: fireStorePost, completion: { error in
                                if error != nil {
                                    self.makeAlert(tittleInput: "Error", messageInput: error?.localizedDescription ?? "Error")
                                   
                                } else {
                                    
                                    self.imageView.image = UIImage(named: "x")
                                    self.commentText.text = ""
                                    self.tabBarController?.selectedIndex = 0
                                }
                                
                            })
                      
                        }
                    }
                }
            }
        }     
    }
    

【问题讨论】:

    标签: ios swift xcode uiimageview


    【解决方案1】:

    在您的版本中,您创建了一个新的 UIImage 实例,并基本上比较了两个不相等的对象,因为它们位于不同的内存区域。要比较图片名称,您需要获取一个 url,具体取决于您获取图像的方式,您可以从中提取文件名。

    如果您使用 UIImagePickerController 来检索图像,那么委托方法的实现将如下所示:

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) 
     {
        let url = info[.imageURL] as? URL
        if let name = url?.lastPathComponent {
           print("Name: (name)")
        }
        
     }
    

    【讨论】:

      【解决方案2】:

      您的 if 声明看起来不正确:

      if self.imageView.image == UIImage(named: "x") && self.commentText.text! == "" {
      

      说的是:

      • 如果
        • imageView 的图像等于UIImage(命名:“x”)
        • commentText 字段(或标签?)等于""(一个空字符串)

      我想你想要这个:

      if self.imageView.image == UIImage(named: "x") || self.commentText.text! == "" {
      

      其中说:

      • 如果
        • imageView 的图像等于UIImage(命名:“x”)
          • 或者
        • commentText 字段(或标签?)等于""(一个空字符串)

      【讨论】:

        猜你喜欢
        • 2022-11-17
        • 2015-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-07
        • 2021-06-17
        • 2017-09-08
        • 1970-01-01
        相关资源
        最近更新 更多