尝试运行任务时,发送图像,错误代码为13“查询已取消”

时间:2018-10-12 11:12:44

标签: php ios swift uiimagepickercontroller nsurlsessionuploadtask

我目前正在使用iOS应用程序的图像选择器从相机角色获取图片,然后尝试将其发送到Web服务器上的PHP脚本以将图像保存在另一端。

但是,当代码行b.setBorder(new RoundedLineBorder(Color.BLACK, 1, 10, true)); 运行时,抛出此错误:

  

发现扩展时遇到[发现]错误:错误域= PlugInKit代码= 13“查询已取消” UserInfo = {NSLocalizedDescription =查询已取消}

这种情况发生后,图像也不会保存在目的地。

我已经阅读了有关此错误的其他几篇文章,并尝试使用task.resume()didFinishPickingMediaWithInfo暴露给Objective-c。

我在plist中分配了“隐私-图片库使用说明”。我还允许任意加载。

通过所有这些错误仍然会发生,因此,如果有人可以发现我的代码有任何错误,将大有帮助。我还将添加PHP脚本,以便您可以看到数据发送到的位置,谢谢。

@objc

PHP脚本:

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, URLSessionDelegate, URLSessionTaskDelegate, URLSessionDataDelegate {

    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var uploadButton: UIButton!
    @IBOutlet weak var progressView: UIProgressView!
    @IBOutlet weak var progressLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        checkPermission()
    }

    func checkPermission() {
        let photoAuthorizationStatus = PHPhotoLibrary.authorizationStatus()
        switch photoAuthorizationStatus {
        case .authorized:
            print("Access is granted by user")
        case .notDetermined:
            PHPhotoLibrary.requestAuthorization({
                (newStatus) in
                print("status is \(newStatus)")
                if newStatus ==  PHAuthorizationStatus.authorized {
                    /* do stuff here */
                    print("success")
                }
            })
            print("It is not determined until now")
        case .restricted:
            // same same
            print("User do not have access to photo album.")
        case .denied:
            // same same
            print("User has denied the permission.")
        }
    }

    @IBAction func uploadTapped(_ sender: UIButton) {
        let pickerController = UIImagePickerController()
        pickerController.delegate = self
        pickerController.sourceType = UIImagePickerController.SourceType.photoLibrary

        self.present(pickerController, animated: true, completion: nil)
    }

    @objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        imageView.image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage

        imageView.backgroundColor = UIColor.clear
        self.dismiss(animated: true, completion: nil)

        uploadImage()
    }

    func uploadImage() {
        let imageData = imageView.image!.jpegData(compressionQuality: 1)

        if imageData == nil {
            return
        }

        self.uploadButton.isEnabled = false

        let uploadScriptURL = NSURL(string: "I've removed the URL for this question, but a valid URL to the PHP goes here")
        var request = URLRequest(url: uploadScriptURL! as URL)
        request.httpMethod = "POST"
        request.setValue("Keep-Alive", forHTTPHeaderField: "Connection")

        let configuration = URLSessionConfiguration.default
        let session = URLSession(configuration: configuration, delegate: self, delegateQueue: OperationQueue.main)

        let task = session.uploadTask(with: request, from: imageData!)
        print(imageData!)
        task.resume()
    }

    func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
        let alert = UIAlertController(title: "Alert", message: error?.localizedDescription, preferredStyle: .alert)
        self.present(alert, animated: true, completion: nil)
        self.uploadButton.isEnabled = true
    }

    func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
        let uploadProgress:Float = Float(totalBytesSent) / Float(totalBytesExpectedToSend)
        progressView.progress = uploadProgress
        let progressPercent = Int(uploadProgress * 100)
        progressLabel.text = "\(progressPercent)%"
    }

    func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
        self.uploadButton.isEnabled = true
    }
}

1 个答案:

答案 0 :(得分:0)

所以事实证明我的问题不是由此错误引起的,并且该错误实际上对代码没有任何影响-似乎可以忽略。

就我而言,由于我的疏忽,代码无法正常工作。我尝试发送的图像为2.2MB,但由于我正在测试的服务器上的上限为2MB,所以我没有意识到它正在返回HTML错误代码413。

如果您在执行此类任务时遇到问题,请确保在urlSession的response函数中打印出didReceive response。这样,您可以检查返回的任何HTML错误。