我將多個圖像上傳到服務器,使用將圖像轉換爲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 –