错误域= NSURLErrorDomain代码= -999“已取消”

时间:2019-06-29 22:52:13

标签: swift

我想使用Google API恢复图像,但该错误不起作用:错误域= NSURLErrorDomain代码= -999出现“取消”,但在某些情况下它可以工作,而我不知道为什么。

screenshot of the console

func getImagePlace(photoreference: String, callback: @escaping (Bool, UIImage?) -> Void) {
    guard let url = URL(string: "https://maps.googleapis.com/maps/api/place/photo?maxwidth=375&maxheight=150&photoreference=" + photoreference + key) else {
        callback(false, nil)
        return
    }
    task?.cancel()
    task = mapSession.dataTask(with: url) { (photo, response, error) in
        DispatchQueue.main.async {
            guard let data = photo, error == nil else {
                let imageDefault = UIImage(named: "NoImageFound.png")
                callback(true, imageDefault!)
                return
            }
            guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {
                let imageDefault = UIImage(named: "NoImageFound.png")
                callback(true, imageDefault!)
                return
            }
            let image = UIImage(data: data)
            callback(true, image!)
        }
    }
    task?.resume()
}

如果您有主意,我不知道我做错了什么。

0 个答案:

没有答案