Swift - Alamofire - 从photolibrary上传文件

时间:2017-07-09 14:19:07

标签: swift file reference upload alamofire

我正在尝试使用Alamofire从我的照片库实施上传:

|| 0

它打印出来:

0

有人可以解释我如何解决这个问题吗?

谢谢和问候!

1 个答案:

答案 0 :(得分:0)

在Swift 3和Alamofire 4中

以下是使用Alamofire

进行上传的完整实施方法
  1. 将以下内容添加到ViewController类:

    UIImagePickerControllerDelegateUINavigationControllerDelegate

  2. 创建一个按钮:

  3. 首先创建一个按钮并在其中实现以下方法以进行选择器视图

    @IBAction func btnSelectProfileImageClicked(_ sender: Any) {
    
        let ImagePicker = UIImagePickerController()
        ImagePicker.delegate = self
        ImagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
    
        self.present(ImagePicker, animated: true, completion: nil)
    
    
    }
    
    1. 然后实现以下UIPicker方法:

      func imagePickerController( _ picker: UIImagePickerController,didFinishPickingMediaWithInfo info:[String : Any] )
      {Imgprofile.image = info[UIImagePickerControllerOriginalImage] as? UIImage
      self.dismiss(animated: true, completion: nil)}
      
    2. 制作另一个按钮,使用Alamofire将数据传递到URL并向其提供@IBAction个出口:

    3. 输入以下数据

       @IBAction func btnUpdateProfileSelected(_ sender: Any) {
      
          Alamofire.upload(multipartFormData: { (multipartFormData) in
              multipartFormData.append(UIImageJPEGRepresentation(self.Imgprofile.image!, 1)!, withName: "Prescription", fileName: "Profile_Image.jpeg", mimeType: "image/jpeg")
          }, to:" Your URL Here where You want to Upload")
          { (result) in
              switch result {
              case .success(let upload, _, _):
                  print(result)
      
                  upload.uploadProgress(closure: { (progress) in
                      print(progress)
                  })
      
                  upload.responseJSON { response in
                      //print response.result
                      print(response);
                  }
      
              case .failure(let encodingError):
                  print(encodingError);
              }
          }
      }
      

      多数人

      希望这会有所帮助

        

      对于完整示例代码或有任何疑问,请发表评论。我会提供给你   这个示例代码。其中包括获取以及   使用Alamofire上传数据。

      由于