[英]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我需要知道如何 select 多个图像并从一开始就使用 Alamofire 或任何其他方式将它们与另一个参数一起上传到服务器
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 1-> 在 swift 中使用 imgaePicker 2-> 完整的 function 使用您自己的上传图像方式,非常感谢,因为我不明白如何从图像选择器中获取图像
finally I got an easy answer using a pod called OpalImagePicker and the way is so easy using Alamofire:最后,我使用名为 OpalImagePicker 的 pod 得到了一个简单的答案,而且使用 Alamofire 的方法非常简单:
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
}
//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)
}
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.