Image Upload using Alamofire
1 ) Create New Project AlamofireDemo .
2 ) Open Terminal and go to Path of Project Folder .
3 ) init Pod Using Below Command .
$ pod init
4 ) Open Podfile Then Write & Save File:
pod 'Alamofire', '~> 4.4'
5 ) Install Pod Using Below Command in Terminal :
$ pod install
6 ) Now open AlamofireDemo.xcworkspace :
Write Below Coding For Get and Post :
import UIKit
import Alamofire
import MobileCoreServices
class ViewController: UIViewController,UINavigationControllerDelegate, UIImagePickerControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - UIButton Actions
@IBAction func profileImgBtnAction(_ sender: Any)
{
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary){
let imag = UIImagePickerController()
imag.delegate = self
imag.sourceType = UIImagePickerControllerSourceType.photoLibrary;
//imag.mediaTypes = [kUTTypeImage];
imag.allowsEditing = true
self.present(imag, animated: true, completion: nil)
}
}
// MARK: - UIImagePickerControllerDelegate Methods
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
imgProfileObj.image = image
} else{
print("Something went wrong")
}
self.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController)
{
dismiss(animated: true, completion: nil)
}
// ------------------- Upload Method -------------------------
func PostUpdateProfile()
{
SwiftLoader.show(title: "Loading...", animated: true)
if let imageData: Data = (UIImageJPEGRepresentation(self.imgProfileObj.image!, 1.0) as Data?) {
let params: Parameters = [
"role_id": "8",
]
let URL = "\(Constant.UserUpdateProfileURL)" // @"https://....."
Alamofire.upload(multipartFormData: {
multipartFormData in
multipartFormData.append(imageData, withName: "user_image", fileName: "abc.jpg", mimeType: "image/jpg")
for (key, value) in params {
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
}, to: URL, method: .post, encodingCompletion: {
(result) in
switch result {
case .success(let upload, _, _):
upload.responseJSON { response in
SwiftLoader.hide()
print(response.timeline)
print("Request: \(String(describing: response.request))") // original url request
print("Error: \(String(describing: response.error))")
print("Result: \(response.result)") // response serialization result
if(response.error == nil)
{
self.sqlObject.createAndOpenDatabase()
if let json = response.result.value as? [String: AnyObject]
{
print("JSON: \(json)")
if (json["status"] as? String == "success")
{
print(json["status"]!)
if (self.sqlObject.getAllUserInfo().value(forKey: "SchoolName") == nil)
{
self.sqlObject.insertKey(toDatabase: "SchoolName", value: json["school"] as! String)
}
else
{
self.sqlObject.updateData(json["school"] as! String , "SchoolName")
}
Constant.showAlert(vc: self, titleStr: (json["msg"] as? String)!, messageStr: "")
}
else
{
print(json["status"]!)
Constant.showAlert(vc: self, titleStr: (json["msg"] as? String)!, messageStr: "")
}
}
}
}
case .failure(let encodingError):
print(encodingError)
SwiftLoader.hide()
Constant.showAlert(vc: self, titleStr: "Error !", messageStr: "No Internet Connection.")
}
})
}
}
}
No comments:
Post a Comment