我正在使用REST API(https://restcountries.eu/),并希望下载标志图像(.svg)并将其显示为UIImage
。我尝试了以下标准方式:
func requestData(at url: URL, success: @escaping (_ data: Data) -> Void, failure: ((_ error: NetworkError) -> Void)? = nil) {
let request = URLRequest(url: url)
let task = URLSession.shared.dataTask(with: request) { (responseData, response, responseError) in
DispatchQueue.main.async {
if responseError != nil {
failure?(.failedRequest)
} else if let data = responseData {
success(data)
} else {
failure?(.corruptedData)
}
}
}
task.resume()
}
并且数据下载正常,但是当我尝试使用UIImage(data: data)
显示图像时,图像为nil
。我想念什么吗?