Swift 多个上传文件或图片
Swift multiple upload files or images
我必须从图库或相机多次上传 images/videos 以及一个文件到我的网站托管。
有人可以帮助我吗?
我尝试了很多代码但没有用,抱歉我是 Swift 的新手。
我在 Android 完成了,现在 iOS 需要它,我发现很难找到关于它的教程。
我刚找到这个 tutorial。
但如果有人可以帮助我,我不知道如何将它放入我的代码中。
并且 ELCImagePickerController
没有通过 pods 文件安装,一个人通过 Facebook 告诉我它太旧了
您可以使用此 https://github.com/opalorange/OpalImagePicker 从图库中多次上传 images/videos。
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)
}
}
}
}
我必须从图库或相机多次上传 images/videos 以及一个文件到我的网站托管。
有人可以帮助我吗?
我尝试了很多代码但没有用,抱歉我是 Swift 的新手。
我在 Android 完成了,现在 iOS 需要它,我发现很难找到关于它的教程。
我刚找到这个 tutorial。
但如果有人可以帮助我,我不知道如何将它放入我的代码中。
并且 ELCImagePickerController
没有通过 pods 文件安装,一个人通过 Facebook 告诉我它太旧了
您可以使用此 https://github.com/opalorange/OpalImagePicker 从图库中多次上传 images/videos。
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)
}
}
}
}