I'm building an app that can send a .jpeg
files to the server. So what I want to do is I have a controller to take a photo and send the photo to the server. Of course the first photo format is a UIImage
, which would not work when I send it to the server because the server doesn't accept UIImage
file types. So I've done a bit of a research and found the function to convert UIImage
to .jpeg
file format like this:
let photoJpg = self.photo?.jpegData(compressionQuality: 1.0)
And I pass the photoJpg
variable to the API call function, but the API keeps rejecting my request, by saying 500 status code when calling the API. So I'm guessing I might not convert the file the right way. So how can I convert the UIImage
to a .jpeg
file format, store it into a variable and send it to the API? If you need more info feel free to ask and I will provide it to you. Thank you.
EDIT: The function to call the API
func submitPhoto() {
let headers = ["authorization": registerData!.token]
let photoJpg = self.photo?.jpegData(compressionQuality: 1.0)
let params: [String: Any] = [
"files": photoJpg!
]
Webservice.Request(url: Webservice.SUBMIT_PHOTO, method: Webservice.HttpMethod.post, parameters: params, headers: headers, loading: true, loadingString: nil, success: { (response) in
print("Submit photo success")
}) { (response) in
print("Submit photo failed")
}
}
The Request function in Webservice class
class func Request(url: String, method: HttpMethod, parameters: [String: Any], headers: [String:String], loading: Bool, loadingString: String?, success: @escaping (JSON) -> Void, failure: @escaping (Error) -> Void) {
if manager == nil {
let urlConfig = URLSessionConfiguration.default
urlConfig.timeoutIntervalForRequest = KPMConfig.RTO_TIME
manager = SessionManager(configuration: urlConfig)
}
let request = manager.request(url, method: method == .post ? HTTPMethod.post : HTTPMethod.get, parameters: parameters, encoding: URLEncoding.default, headers: headers)
if loading {
ProgressHUD.show(loadingString, interaction: false)
}
print("-url: \n\(url)")
print("-params: \n\(parameters)")
print("-headers: \n\(headers)")
request.responseJSON { (response) in
if loading {
ProgressHUD.dismiss()
}
switch response.result {
case .success(let values):
let data = JSON(values)
print("response: \n\(data)")
switch data["code"].intValue {
case 200...203:
success(data)
case 400...403:
let errorDetails = Error(
error: ErrorModel(code: data["error"]["code"].stringValue,
message: data["error"]["message"].stringValue,
target: data["error"]["target"].stringValue,
details: data["error"]["details"]),
code: data["code"].intValue)
failure(errorDetails)
if data["code"].intValue == 401 {
print("-url: \n\(url)")
self.removeUserData()
self.goToLoginPage()
}
case 500:
let errorDetails = Error(
error: ErrorModel(code: data["error"]["code"].stringValue,
message: data["error"]["message"].stringValue,
target: data["error"]["target"].stringValue,
details: data["error"]["details"]),
code: data["code"].intValue)
failure(errorDetails)
default:
break
}
case .failure(let values):
let data = JSON(values)
print("-response: \n\(data)")
let errorDetails = Error(
error: ErrorModel(code: data["error"]["code"].stringValue,
message: data["error"]["message"].stringValue,
target: data["error"]["target"].stringValue,
details: data["error"]["details"]),
code: data["code"].intValue)
failure(errorDetails)
}
}
}