我正在尝试使用alamofire将图像上传到PHP服务器。实际上图像已成功上传到服务器,但是当我使用相同的链接/图像路径获取数据时,我总是会返回旧图像,而不是我刚刚上传的新图像。 服务器上实际上不再提供旧图像,但它似乎在应用中缓存/保留。
这是我用于使用alamofire上传图像并将图像下载回来的代码
struct NetworkingService {
static func fetchData(url: URL, completion: @escaping (APIResult<Data>) -> Void) {
// this function will be used to download image data from the server
Alamofire.SessionManager.default
.requestWithoutCache("http://localhost/Twitter/Avatar/53/avatar.jpeg").response { response in
print("Request: \(response.request!)")
print("Response: \(response.response!)")
print("Error: \(response.error!)") <--- i got a fatal error in here
}
let request = URLRequest(url:url)
Alamofire.request(request).responseData { (response) in
if response.error != nil {
print(response.error!)
completion(.failure(response.error!))
}
guard let data = response.data else {return}
completion(.success(data)) <-- the Data will be used to generate UIImage later
}
}
static func uploadAvatar (image: UIImage, endPoint: EndPoint, completion : @escaping (APIResult<Any>) -> Void ) {
let urlString = "\(endPoint.baseURL)\(endPoint.path)"
let imgData = UIImageJPEGRepresentation(image, 0.5)!
let url = try! URLRequest(url: URL(string: urlString)!, method: .post, headers: nil)
Alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(imgData, withName: "file", fileName: "avatar.jpeg", mimeType: "image/jpeg")
for (key, value) in endPoint.parameters {
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
},
with: url,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
if((response.result.value) != nil) {
guard let json = response.result.value as? [String:Any] else {return}
guard let id = json["id"] as? String,
let username = json["username"] as? String,
let fullname = json["fullname"] as? String,
let email = json["email"] as? String,
let avatar = json["avatar"] as? String else {
return
}
let userData : [String : Any] = [
"userID" : id,
"username" : username,
"email" : email,
"avatar" : avatar,
"fullname" : fullname
]
completion(.success(userData))
} else {
}
}
case .failure(let encodingError):
completion(.failure(encodingError))
print(encodingError)
break
}
}
)
}
extension Alamofire.SessionManager {
@discardableResult
open func requestWithoutCache(
_ url: URLConvertible,
method: HTTPMethod = .get,
parameters: Parameters? = nil,
encoding: ParameterEncoding = URLEncoding.default,
headers: HTTPHeaders? = nil)// also you can add URLRequest.CachePolicy here as parameter
-> DataRequest
{
do {
var urlRequest = try URLRequest(url: url, method: method, headers: headers)
urlRequest.cachePolicy = .reloadIgnoringCacheData // <<== Cache disabled
let encodedURLRequest = try encoding.encode(urlRequest, with: parameters)
return request(encodedURLRequest)
} catch {
// TODO: find a better way to handle error
print(error)
return request(URLRequest(url: URL(string: "http://example.com/wrong_request")!))
}
}
}
我在why i get different image when downloading to the same path?
找到了类似我的问题的案例据说我必须使用此代码删除所有缓存策略
let session = URLSession(configuration: .ephemeral)
如果这是问题,那么我如何使用alamofire实现这一目标?因为上面的解决方案是使用本机URL会话功能
我试图在这个中实现解决方案,但它不起作用 How to disable caching in Alamofire
提前致谢:)