我正在实施iOS应用,并在将照片上传到服务器时尝试执行以下操作:
我无法实现这一点,尤其是在显示响应消息2秒后解除视图。这是我的代码。 你好吗? 感谢。
class ShareViewController: UIViewController {
var progressHUD: MBProgressHUD! = nil
func sendToServer(image: UIImage, imageName: String) {
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(jpgImageData!, withName: "photos",fileName: fname, mimeType: mime)
for (key, value) in parameters {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
},
to:url!)
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
DispatchQueue.main.async(execute: {
self.progressHUD.show(animated: true)
self.progressHUD.progress = (Float(progress.fractionCompleted))
self.progressHUD.hide(animated: true, afterDelay: 3)
})
})
upload.responseJSON { response in
if response.response?.statusCode == 200{
if let result = response.result.value {
var message = String()
message = "200, OK"
self.progressHUD.label.text = message
self.progressHUD.show(animated: true)
self.progressHUD.hide(animated: true, afterDelay: 2)
DispatchQueue.main.async(execute: {
self.extensionContext?.completeRequest(returningItems: nil, completionHandler: nil)
})
}
}
else{
var message = String()
message = "Not 200, NOK"
self.progressHUD.label.text = message
self.progressHUD.show(animated: true)
self.progressHUD.hide(animated: true, afterDelay: 2)
DispatchQueue.main.async(execute: {
self.extensionContext?.completeRequest(returningItems: nil, completionHandler: nil)
})
}
}
case .failure(let encodingError):
print(encodingError)
var message = String()
message = "Failure, NOK"
self.progressHUD.label.text = message
self.progressHUD.show(animated: true)
self.progressHUD.hide(animated: true, afterDelay: 2)
DispatchQueue.main.async(execute: {
self.extensionContext?.completeRequest(returningItems: nil, completionHandler: nil)
})
}
}
}
}
答案 0 :(得分:0)
我找到了一个效果很好的解决方案。
解散视图的最后一个DispatchQueue
不会在2秒后立即执行。这是代码:
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.extensionContext?.completeRequest(returningItems: nil, completionHandler: nil)
}
我希望有所帮助。