I am uploading an image on button press using Alamofire
, but it show an ERROR at
AF.upload(multipartFormData: {
MultipartFormData line of method
(Note : Alamofire.upload is change to AF.upload After Alamofire 5.0 Update)
@IBAction func btnUploadImage(_ sender: UIButton) {
let uploadDict = ["user_id":getUserId] as [String:String]
AF.upload(multipartFormData: { MultipartFormData in
let image :Data = UIImageJPEGRepresentation(self.uploadImg.image!, 1.0)!
MultipartFormData.append(image, withName: "image" , fileName: "image.jpeg" , mimeType: "image/jpeg")
for(key,value) in uploadDict{
MultipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)}
}, to: "http://XXXXXXXXXXXXXXXX/uploadImage", encodingCompletion: {
EncodingResult in
switch EncodingResult{
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint("SUCCESS RESPONSE: \(response)")
}
case .failure(let encodingError):
print("ERROR RESPONSE: \(encodingError)")
} })
//View Controller for this code
import UIKit
import Alamofire
class updateVC: UIViewController, UIImagePickerControllerDelegate,UINavigationControllerDelegate {
//MARK:- OUTLETS
@IBOutlet weak var tFeditName: UITextField!
@IBOutlet weak var tFusername: UITextField!
@IBOutlet weak var tFemail: UITextField!
@IBOutlet weak var uploadImg: UIImageView!
@IBOutlet weak var tVeditAddress: UITextView!
//MARK:- DECLARATIONS
var imagePicker = UIImagePickerController()
var editName = String()
var editUserName = String()
var editEmail = String()
var editImage = UIImage()
var editAddress = String()
var getUserId = String()
//MARK:- LIFE CYCLE
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
tFeditName.text = editName
tFusername.text = editUserName
tFemail.text = editEmail
uploadImg.image = editImage
tVeditAddress.text = editAddress
}
//MARK:- CUSTOM FUNCTIONS
func postJSON() {
let param = ["fullname":tFeditName.text!,"username":tFusername.text!,"email":tFemail.text!,"address":tVeditAddress.text!,"user_id":getUserId] as NSDictionary
AF.request("http://XXXXXXXXXXXXXXXXXXXX/updatedetails", method: .post, parameters: param as? Parameters, encoding: URLEncoding.default).responseJSON { response in
switch response.result{
case .success(let json):
print(json)
DispatchQueue.main.async {
print(param)
}
case.failure(let Error):
print(Error)
}
}
}
//ImageViewPicker
func openCamera()
{
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerController.SourceType.camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
else
{
let alert = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
func openGallery()
{
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.allowsEditing = true
imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
self.present(imagePicker, animated: true, completion: nil)
}
else
{
let alert = UIAlertController(title: "Warning", message: "You don't have permission to access gallery.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
internal func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
uploadImg.image = pickedImage
}
picker.dismiss(animated: true, completion: nil)
}
//MARK:- ACTIONS
@IBAction func btnSelectImage(_ sender: UIButton) {
let alert = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { _ in
self.openCamera()
}))
alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { _ in
self.openGallery()
}))
alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
@IBAction func btnUploadImage(_ sender: UIButton) {
let image: UIImage? = self.uploadImg.image
let uploadDict = ["user_id":getUserId] as [String:String]
AF.upload(multipartFormData: { MultipartFormData in
let image :Data = (image?.jpegData(compressionQuality: 1))!
MultipartFormData.append(image, withName: "image" , fileName: "image.jpeg" , mimeType: "image/jpeg")
for(key,value) in uploadDict{
MultipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)}
}, to: "http://XXXXXXXXXXXXXXXX/uploadImage", encodingCompletion: {
EncodingResult in
switch EncodingResult{
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint("SUCCESS RESPONSE: \(response)")
}
case .failure(let encodingError):
print("ERROR RESPONSE: \(encodingError)")
} })
}
@IBAction func btnUpdateDetails(_ sender: UIButton) {
postJSON()
self.navigationController?.popViewController(animated: true)
}
}
From Alamofire 5.0 Migration Guide:
- MulitpartFormData’s API has changed and the top level upload methods to create and upload MultipartFormData have been updated to match other request APIs, so it’s not longer necessary to deal with the Result of the multipart encoding.
So your code should be like this:
@IBAction func btnUploadImage(_ sender: UIButton) {
let uploadDict = ["user_id": "getUserId"] as [String:String]
AF.upload(multipartFormData: { MultipartFormData in
let image: Data = self.uploadImg.image!.jpegData(compressionQuality: 1.0)!
MultipartFormData.append(image, withName: "image" , fileName: "image.jpeg" , mimeType: "image/jpeg")
for(key,value) in uploadDict {
MultipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}, to: "http://XXXXXXXXXXXXXXXX/uploadImage", method: .post, headers: ["Content-Type": "application/json",
"authorization": "bearer \(token)"])
.responseJSON { (response) in
debugPrint("SUCCESS RESPONSE: \(response)")
}
}
So you will not need encodingCompletion
closure and use responseJSON immediately.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With