Ниже упомянут мой метод загрузки многокомпонентного изображения на сервер, но когда я пытаюсь это сделать, приложение вылетает "неожиданно найден ноль"
Но ценность изображения там, так как я выбираю его из библиотеки фотографий.
func createCoupon(_ code: String, storeID: Int, description: String, terms: String, image: UIImage, startDate: String, endDate: String, couponDiscount: String, minimumDiscount: String, percentage: String, maximumDiscount: String){
let urlString = BaseURL + "create-coupon"
let params =
[
"code" : code,
"store_id" : storeID,
"type" : "merchant",
"description" : description,
"terms" : terms,
"start_date" : startDate,
"end_date" : endDate,
"coupon_discount" : couponDiscount,
"minimum_total" : minimumDiscount,
"percentage" : percentage,
"maximum_discount" : maximumDiscount
] as [String : Any]
let manager = Alamofire.SessionManager.default
manager.session.configuration.timeoutIntervalForRequest = 30000
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(UIImageJPEGRepresentation(image, 0.2)!, withName: "image", fileName: "swift_file.jpeg", mimeType: "image/jpeg")
for (key, value) in params {
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
}, to:urlString)
{
(result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
//Print progress
})
upload.responseJSON { response in
let resJson = response.result.value
print(resJson)
NotificationCenter.default.post(name: Notification.Name(rawValue: NotifRequestSuccess.createCoupon.rawValue), object: nil, userInfo: ["data": resJson!])
}
case .failure(let encodingError):
NotificationCenter.default.post(name: Notification.Name(rawValue: NotifRequestError.createCoupon.rawValue), object: encodingError, userInfo: nil)
}
}