Below mentioned is my method to upload a multipart image to the server but when I am trying to do so the app is crashing "unexpectedly found nill"
But value of image is there as I am selecting it from photo library.
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)
}
}
Alamofire4 and Swift 4.1
1st Step: Take a imageView and pick the image by using imagepickerview After that take a button in which below function will be call
**
*
userImage will be ur array or may be dictionary depends upon the response and declare it globally as var userImage = String: AnyObject
Just simple Use this Function as
Function Usage
if you have parameters send Parameters else nil or [:]
Use Below code for single Image Upload using
Alamofire
:Swift 3.0
Below is the function used for uploading single image from
ImagePickerController
and call this method when you click button to uploadImage
I've created a func to satisfy thy needs in this case. Pass the UIImage in 'image' param in the below request.
Below is the code for the function.
Try This