Search code examples
iosswiftiphonepostalamofire

uploading multi images to the server with swift using Alamofire or anyway to achieve that from the first step to know how we use it with image picker


I need to know how can I select multiple images and upload them to the server with another params using Alamofire or any other way from the begining

what I really need to see is how can I use image picker with this way

1-> use imgaePicker in swift 2-> full function using your own way of uploading images and many thanks because I don't understand how can I get images from image picker


Solution

  • finally I got an easy answer using a pod called OpalImagePicker and the way is so easy using Alamofire:

    code from the first to understand

    import Alamofire
    import ImagePicker
    import Photos
    import OpalImagePicker
    
    class PostVC: UIViewController,UITextViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate, ImagePickerDelegate,OpalImagePickerControllerDelegate {
    
    var arryOfImages = [UIImage]()
    
    // you have to put them don't worry about those funcs
    func wrapperDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) {
        print("picked")
    }
    
    func doneButtonDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) {
        print("done")
    
    func cancelButtonDidPress(_ imagePicker: ImagePickerController) {
        print("cancel")
    }
    

    // set in your button action to get images the image/video .. etc and num of them


    @IBAction func addPicture_clicked(_ sender: Any) {
            let imagePicker = OpalImagePickerController()
                    imagePicker.imagePickerDelegate = self
                    imagePicker.maximumSelectionsAllowed = 3
            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: nill
        
    }
    

    The Request You Want

    //finally the request and thank you ^^


    func addPostClicked(){
    
            guard let text = postTextView.text else {
                        return
                    }
            let token = "UYJ9ohx_M6JvDbJu0"
            
            let profileId = 104
            let params : [String: Any] = [
                        "postText" :text,
                        "profileId":profileId
            ]
            
       
                Alamofire.upload(
                    multipartFormData: { multipartFormData in
                        var count = 1
                    for img in self.arryOfImages {
                        //here we send our images
                        let imageData = img.jpegData(compressionQuality: 0.5)
                            multipartFormData.append(imageData!, withName: "images[]", fileName: "image\(count).jpeg", mimeType: "image/jpeg")
                        count += 1
                    }
    
                    for (key, value) in params
                    {
                        multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key)
                        
                    }
                }, to:"https://api.yoogad.com/rest/api/v1/post/create", method:.post, headers: ["x-auth-token" : token],encodingCompletion: { encodingResult in
                switch encodingResult {
                  case .success(let upload, _, _):
                        upload.responseJSON { response in
                        debugPrint(response)
                            self.dismiss(animated: true)
                        }
                  case .failure(let encodingError):
                       print(encodingError)
                  }
                })
               }