我正在尝试使用Alamofire(swift 4.2)上传图像,但是在将图像转换为数据时出现此错误 通话中有额外的参数“ compressionQuality”
代码是
import Foundation
import SwiftyJSON
import Alamofire
class APIUpload {
class func createPhoto(photo: UIImage, completion: @escaping (_ error:Error?, _ success: Bool)->Void){
let url = "http://www.fb.test/photo.php"
Alamofire.upload(multipartFormData: { (form: MultipartFormData) in
let data = photo.jpegData(compressionQuality: 1.0){
form.append(data:Data, withName:"photo",fileName:"photo.jpeg",mimeType:"image/jpeg")
}
}, usingThreshold: SessionManager.multipartFormDataEncodingMemoryThreshold, to: url, method: .post, headers: nil) { (result: SessionManager.MultipartFormDataEncodingResult) in
switch result {
case .failure(let error):
print(error)
completion(error, false)
case .success(request: let upload, streamingFromDisk: _, streamFileURL: _):
upload.uploadProgress(closure: { (progress: Progress) in
print(progress)
})
upload.responseJSON(completionHandler: { (response: DataResponse<Any>) in
switch response.result{
case .failure(let error):
print(error)
case .success(let value):
let json = JSON(value)
print(json)
completion(nil, true)
}
})
}
}
}
}
答案 0 :(得分:-1)
jpegData
的签名
func jpegData(compressionQuality: CGFloat) -> Data?
所以你需要
if let data = photo.jpegData(compressionQuality: 1.0){
form.append(data:data, withName:"photo",fileName:"photo.jpeg",mimeType:"image/jpeg")
}