1

我需要从 URL 下载图像作为数据并将其重新创建为UIImage(data:). 问题是,Alamofire 在第一次请求时只下载少量图像数据:在此处输入图像描述

在我对我的 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)
    }
}
4

1 回答 1

0

SOLVED: The problem was with Alamofire request, I was able to resolve the issue with using AlamofireImage framework.

于 2021-08-25T08:55:40.570 回答