我将多个图像上传到服务器,使用将图像转换为base64并将图像作为参数在API中发送。但是当我们一次又一次地调用api时,如何停止api调用按钮单击。我使用下面的代码来调用API。如何取消URL会话请求
在此先感谢
let urlPath: String = "URL"
let url: URL = URL(string: urlPath)!
var request1 = URLRequest(url: url)
request1.httpMethod = "POST"
let stringPost="imgSrc=\(image)"
let data = stringPost.data(using: String.Encoding.utf8)
// print("data\(data)")
request1.httpBody=data
request1.timeoutInterval = 60
let _:OperationQueue = OperationQueue()
let task = session.dataTask(with: request1){data, response, err in
do
{
if data != nil
{
print("data\(String(describing: data))")
if let jsonResult = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary
{
DispatchQueue.main.async
{
print("json\(jsonResult)")
}
}
}
catch let error as NSError
{
DispatchQueue.main.async
{
print("error is \(error)")
print("error desc \(error.localizedDescription)")
}
}}
task.resume()
的可能的复制[NSURLSession取消任务](https://stackoverflow.com/questions/19975642/nsurlsession-cancel-task) –
重复的可能性https://stackoverflow.com/questions/23518690/how-to-find-and-c安塞尔 - 一个任务功能于nsurlsession –