Alamofire 和从服务器下载图像
Alamofire and downloading images from server
我需要从 URL 下载图像作为数据并将其重新创建到 UIImage(data:)
。问题是,Alamofire 在 first 请求时仅下载少量图像数据:
在我重新调用我的 API 后,整个图像被下载:
我真的不明白为什么会是这样。第一次请求总是无法下载所有数据。这是我用来从 URL:
下载数据的代码
func requestWithData(
_ url: String,
method: HTTPMethod = .get,
parameters: Parameters? = nil,
decoder: JSONDecoder = JSONDecoder(),
headers: HTTPHeaders? = nil,
interceptor: RequestInterceptor? = nil
) -> Future<Data, ServerError> {
return Future({ promise in
AF.request(
url,
method: method,
parameters: parameters,
encoding: JSONEncoding.default,
headers: headers,
interceptor: interceptor ?? self
)
.validate(statusCode: [200, 201, 204, 400, 401])
.responseData(completionHandler: { (response) in
switch response.result {
case .success(let value):
promise(.success(value))
case .failure(let error):
promise(.failure(self.createError(response: response.response, error: error, data: response.data)))
}
})
})
}
func getLocationImage(location: Location) -> Future<Void, ServerError> {
Future { promise in
self.networkManager.requestWithData(Endpoint.locationBadge(locationId: location.id, uuid: location.uuid ?? "").url)
.sink { completion in
if case .failure(let error) = completion {
promise(.failure(error))
}
} receiveValue: { [unowned self] imageData in
self.updateLocationImage(location: location, image: imageData)
.sink { completion in
if case .failure(let error) = completion {
promise(.failure(.init(message: "Error while saving the data", code: .coreData, args: [error.localizedDescription])))
}
} receiveValue: { _ in
promise(.success(()))
}
.store(in: &subscription)
}
.store(in: &self.subscription)
}
}
已解决:问题出在 Alamofire 请求上,我可以使用 AlamofireImage framework.
解决问题
我需要从 URL 下载图像作为数据并将其重新创建到 UIImage(data:)
。问题是,Alamofire 在 first 请求时仅下载少量图像数据:
在我重新调用我的 API 后,整个图像被下载:
我真的不明白为什么会是这样。第一次请求总是无法下载所有数据。这是我用来从 URL:
下载数据的代码func requestWithData(
_ url: String,
method: HTTPMethod = .get,
parameters: Parameters? = nil,
decoder: JSONDecoder = JSONDecoder(),
headers: HTTPHeaders? = nil,
interceptor: RequestInterceptor? = nil
) -> Future<Data, ServerError> {
return Future({ promise in
AF.request(
url,
method: method,
parameters: parameters,
encoding: JSONEncoding.default,
headers: headers,
interceptor: interceptor ?? self
)
.validate(statusCode: [200, 201, 204, 400, 401])
.responseData(completionHandler: { (response) in
switch response.result {
case .success(let value):
promise(.success(value))
case .failure(let error):
promise(.failure(self.createError(response: response.response, error: error, data: response.data)))
}
})
})
}
func getLocationImage(location: Location) -> Future<Void, ServerError> {
Future { promise in
self.networkManager.requestWithData(Endpoint.locationBadge(locationId: location.id, uuid: location.uuid ?? "").url)
.sink { completion in
if case .failure(let error) = completion {
promise(.failure(error))
}
} receiveValue: { [unowned self] imageData in
self.updateLocationImage(location: location, image: imageData)
.sink { completion in
if case .failure(let error) = completion {
promise(.failure(.init(message: "Error while saving the data", code: .coreData, args: [error.localizedDescription])))
}
} receiveValue: { _ in
promise(.success(()))
}
.store(in: &subscription)
}
.store(in: &self.subscription)
}
}
已解决:问题出在 Alamofire 请求上,我可以使用 AlamofireImage framework.
解决问题