UIImagePickerController无法启动下一个功能

问题描述 投票:1回答:1

我正在使用Swift 5,并将MessageKit集成到我的应用程序中。这是预期的功能:

  1. 用户通过选择器选择图像
  2. uploadImage函数已启动
  3. 图像已上传并显示

实际发生的事情:

  1. 用户通过选择器选择图像
  2. 从那里什么都没发生。

我没有收到一条错误消息。我已经输入了print语句,以查看它是否甚至正在输入uploadImage()函数,但尚未启动。我已经将uipickerimagecontroller代码更改为在我应用的其他位置正常运行的确切代码,甚至还没有启动该功能。我知道我的代码不太漂亮,但我仍在学习(请不要判断笑)。谁能帮忙:

可变设置

private var isSendingPhoto = true {
  didSet {
    DispatchQueue.main.async {
      self.messageInputBar.leftStackViewItems.forEach { item in
        item.inputBarAccessoryView?.isUserInteractionEnabled = !self.isSendingPhoto
      }
    }
  }
}

ViewDidLoad

    messagesCollectionView.messagesDataSource = self
    messagesCollectionView.messagesLayoutDelegate = self
    messageInputBar.delegate = self as? InputBarAccessoryViewDelegate
    messagesCollectionView.messagesDisplayDelegate = self
    title = "MaybeDrinks"

    // 1
    let cameraItem = InputBarButtonItem(type: .system)
    cameraItem.tintColor = .primary
    cameraItem.image = #imageLiteral(resourceName: "camera")

    // 2
    cameraItem.addTarget(
      self,
      action: #selector(cameraButtonPressed),
      for: .primaryActionTriggered
    )
    cameraItem.setSize(CGSize(width: 60, height: 30), animated: false)

    messageInputBar.leftStackView.alignment = .center
    messageInputBar.setLeftStackViewWidthConstant(to: 50, animated: false)
    messageInputBar.setStackViewItems([cameraItem], forStack: .left, animated: false) // 3
}

    private func sendPhoto(_ image: UIImage) {
          isSendingPhoto = true

          uploadImage(image) { [weak self] url in
            guard let `self` = self else {
              return
            }
            self.isSendingPhoto = false

            guard let url = url else {
              return
            }

            var message = Message(messageuser: self.sender, image: image)
            message.downloadURL = url

            self.save(message)
            self.messagesCollectionView.scrollToBottom()
          }
        }
    func imagePickerController(_ picker: UIImagePickerController,
                                   didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
          picker.dismiss(animated: true, completion: nil)

          // 1
          if let asset = info[.phAsset] as? PHAsset {
            let size = CGSize(width: 500, height: 500)
            PHImageManager.default().requestImage(
              for: asset,
              targetSize: size,
              contentMode: .aspectFit,
              options: nil) { result, info in
                print("I'm in image picker")
              guard let image = result else {
                return
              }

              self.sendPhoto(image)
            }

          // 2
          } else if let image = info[.originalImage] as? UIImage {
            sendPhoto(image)
          }
        }

        func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
          picker.dismiss(animated: true, completion: nil)
        }
        // MARK: - Actions

        @objc private func cameraButtonPressed() {
          let picker = UIImagePickerController()
            picker.delegate = self as? UIImagePickerControllerDelegate & UINavigationControllerDelegate
          picker.allowsEditing = true

          if UIImagePickerController.isSourceTypeAvailable(.camera) {
            picker.sourceType = .camera
          } else {
            picker.sourceType = .photoLibrary
          }

          present(picker, animated: true, completion: nil)
        }

上传图像功能

private func uploadImage(_ image: UIImage, completion: @escaping (URL?) -> Void) {
  print("im in upload")

  // STEP 1. Declare URL, Request and Params
  let url = URL(string: "https://localhost/messagepost.php")!

  // declaring reqeust with further configs
  var request = URLRequest(url: url)

  // POST - safest method of passing data to the server
  request.httpMethod = "POST"

         // values to be sent to the server under keys (e.g. ID, TYPE)
  let params = ["sender_id": user_id, "uuid": uuid, "sender": me, "recipient_id": rid, "recipient": recipient, "puuid": puuid]

  // body
  let boundary = "Boundary-\(UUID().uuidString)"
  request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")

  // Compressing image and converting image to 'Data' type
  guard let scaledImage = image.scaledToSafeUploadSize,
    let data = scaledImage.jpegData(compressionQuality: 0.4) else {
    return
  }

  // assigning full body to the request to be sent to the server
  request.httpBody = createBodyWithParams(params, filePathKey: "file", imageDataKey: data, boundary: boundary)
    print(request.httpBody as Any, "\(puuid).jpg")

  URLSession.shared.dataTask(with: request) { (data, response, error) in
      DispatchQueue.main.async {

          // error occured
          if error != nil {
              Helper().showAlert(title: "Server Error", message: error!.localizedDescription, in: self)
              return
          }


          do {

              // save mode of casting any data
              guard let data = data else {
                  Helper().showAlert(title: "Data Error", message: error!.localizedDescription, in: self)
                  return
              }

              // fetching JSON generated by the server - php file
              let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? NSDictionary

              // save method of accessing json constant
              guard let parsedJSON = json else {
                  return
              }

              // uploaded successfully
              if parsedJSON["status"] as! String == "200" {
                let newurl = parsedJSON["path"]
                print("did you upload", newurl as Any)
                print("did you upload", parsedJSON["message"] as Any)
                self.isSendingPhoto = true
                  guard let url = newurl else {
                    return
                  }
                  //uploadImage(image)
                var message = Message(messageuser: self.sender, image: image)
                message.downloadURL = url as? URL

                    self.save(message)
                    self.messagesCollectionView.scrollToBottom()


              } else {

                  // show the error message in AlertView
                  if parsedJSON["message"] != nil {
                      let message = parsedJSON["message"] as! String
                      Helper().showAlert(title: "Error", message: message, in: self)
                    print("where am i", parsedJSON["message"] as Any)
                  }

              }

          } catch {
              Helper().showAlert(title: "JSON Error", message: error.localizedDescription, in: self)
            print("where am i 2")
          }

      }
  }.resume()

  let imageName = [UUID().uuidString, String(Date().timeIntervalSince1970)].joined()


}

private func save(_ message: Message) {


    self.messagesCollectionView.scrollToBottom()

}
swift uiimagepickercontroller swift5 messagekit
1个回答
1
投票

确保您的视图控制器在任何一个类声明中都声明同时符合UIImagePickerControllerDelegateUINavigationControllerDelegate

class DirectMessageViewController: MessagesViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    [...]
}

或一个或多个扩展名:

extension DirectMessageViewController: UIImagePickerControllerDelegate {
    [...]
}

extension DirectMessageViewController: UINavigationControllerDelegate {
    [...]
}
© www.soinside.com 2019 - 2024. All rights reserved.