快速上传多个上传文件或图像

问题描述 投票:0回答:1

我必须从图库或照相机中上传多个图像/视频,以及一个文件到我的网站托管中。

有人可以帮助我吗?

我尝试了很多代码,但是没有用,对不起,我是Swift的新手。

我现在是在Android上制作的,而iOS则需要它,我发现很难找到有关它的教程。

我刚刚找到了此tutorial

但是如果有人可以帮助我,不知道如何将其放入我的代码中。

[ELCImagePickerController没有通过pods文件安装,一个人通过Facebook告诉我,它太老了

ios swift xcode image image-uploading
1个回答
0
投票

您可以使用此https://github.com/opalorange/OpalImagePicker从图库中上传多个图像/视频。

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)
        }
    }
  }
}
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.