Search code examples
iosswiftxcodeimageimage-uploading

Swift multiple upload files or images


I have to multiple upload images/videos from a gallery or camera and also a file to my website hosting.

Somebody can help me?

I tried many codes but don't work, sorry I'm new to Swift.

I made it in Android now I need it for iOS and I saw it is very difficult to find a tutorial about it.

I just found this tutorial.

But don't know how to put it in my code if someone can help me.

And ELCImagePickerController not installed through pods file, a guy told me through Facebook it is too older


Solution

  • You can use this https://github.com/opalorange/OpalImagePicker multiple upload images/videos from a gallery.

    import UIKit
    import OpalImagePicker  
    
    class MultipleImagesUploadVC: UIViewController,OpalImagePickerControllerDelegate {
    
    // MARK: - Variable Declaration
    var arryOfImages = [UIImage]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
           let imagePicker = OpalImagePickerController()
            imagePicker.imagePickerDelegate = self
            imagePicker.maximumSelectionsAllowed = 1
            imagePicker.allowedMediaTypes = Set([PHAssetMediaType.image]) //you can select only images set this
            present(imagePicker, animated: true, completion: nil)
    }
    func imagePicker(_ picker: OpalImagePickerController, didFinishPickingImages images: [UIImage]){
        //print(images)
         self.arryOfImages = images   
        picker.dismiss(animated: true, completion: nil)
        self.singUpAPI()
    }
    
    func singUpAPI(){
        let params:NSMutableDictionary = [:] //Optional for extra parameter
        print(params.dict2json())
        SVProgressHUD.show()
        Alamofire.upload(
            multipartFormData: { multipartFormData in
    
            for imageData in self.arryOfImages {
                    multipartFormData.append(imageData.jpegData(compressionQuality: 0.1)!, withName: "images[1][]", fileName: "\(Date().timeIntervalSince1970).jpeg", mimeType: "image/jpeg")
            }
    
            for (key, value) in params
            {
                multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key as? String ?? "")
            }
        }, to:ServerAPI.KSignup, method:.post, headers: ["Accept" : "application/json","Content-Type": "application/json; charset=utf-8"]) { (result) in
            switch result {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    guard response.result.isSuccess else {
                        print(response.error?.localizedDescription ?? "Error while requesting")
                        return
                    }
                    if let jsonResponse = response.result.value as? [String: Any] {
                        print(jsonResponse)
                        SVProgressHUD.dismiss()
                        //let dict = jsonResponse["result"] as? NSDictionary ?? [:]
    
                        let msg = jsonResponse["message"] as? String ?? ""
                        let status = jsonResponse["status"] as? Int ?? 0
                        SVProgressHUD.dismiss()
                        if status == 1{
                            self.showAlert(title: Appname, message: msg)
                        }else if status == 3{
                            self.ShowAlertWithResult(title: Appname, message: "Session expired please login again", isYesNo: false, yesTitle: "Login", noTitle: "", completion: { (result) in
                                if result == true{
                                    let welcomeVC = MainStoryboard.instantiateViewController(withIdentifier: "WelComeVC") as! WelComeVC
                                    self.navigationController?.pushViewController(welcomeVC, animated: true)
                                }
                            })
                        }else{
                            self.showAlert(title: Appname, message: msg)
                        }
                    }
                }
    
            case .failure(let encodingError):
                print(encodingError)
            }
        }
      }
    }