繁体   English   中英

使用 Alamofire 或无论如何使用 swift 将多张图片上传到服务器,从第一步开始了解我们如何将它与图片选择器一起使用

[英]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

我需要知道如何 select 多个图像并从一开始就使用 Alamofire 或任何其他方式将它们与另一个参数一起上传到服务器

我真正需要看到的是如何以这种方式使用图像选择器

1-> 在 swift 中使用 imgaePicker 2-> 完整的 function 使用您自己的上传图像方式,非常感谢,因为我不明白如何从图像选择器中获取图像

最后,我使用名为 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")
}

// 在您的按钮操作中设置以获取图像图像/视频.. 等以及它们的数量


@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
    
}

您想要的请求

//最后是请求,谢谢^^


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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM