Swift - Alamofire - 从照片库上传文件

Posted

技术标签:

【中文标题】Swift - Alamofire - 从照片库上传文件【英文标题】:Swift - Alamofire - upload file from photolibrary 【发布时间】:2017-07-09 14:19:07 【问题描述】:

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

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) 
    guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else 
        return
    

    let documentDirectory: NSString = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! as NSString

    let imageName = "temp"
    let imagePath = documentDirectory.appendingPathComponent(imageName)

    if let data = UIImageJPEGRepresentation(image, 80) 
        do 
            try data.write(to: URL(fileURLWithPath: imagePath), options: .atomic)
         catch let error 
            print(error)
        
    

    Alamofire.upload(.POST, "\(self.app_url)/user/upload", multipartFormData:  formData in
        let filePath = NSURL(fileURLWithPath: image)
        formData.appendBodyPart(fileURL: filePath, name: "upload")
        formData.appendBodyPart(data: "Alamofire".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name: "test")
    , encodingCompletion:  encodingResult in
        switch encodingResult 
        case .Success:
            print("SUCCESS")
        case .Failure(let error):
            print(error)
        
    )

    self.dismiss(animated: true, completion: nil)

然后打印出来:

Ambiguous reference to member 'upload(_:to:method:headers:)'

谁能解释我如何解决这个问题?

谢谢和问候!

【问题讨论】:

【参考方案1】:

在 Swift 3 和 Alamofire 4 中

这里是如何使用 Alamofire 上传的完整实现

    将以下内容添加到您的 ViewController 类中:

    UIImagePickerControllerDelegateUINavigationControllerDelegate

    创建一个按钮:

首先创建一个按钮并在其中为选取器视图实现以下方法

@IBAction func btnSelectProfileImageClicked(_ sender: Any) 
    
    let ImagePicker = UIImagePickerController()
    ImagePicker.delegate = self
    ImagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
    
    self.present(ImagePicker, animated: true, completion: nil)
    
    

    然后实现以下 UIPicker 方法:

    func imagePickerController( _ picker: UIImagePickerController,didFinishPickingMediaWithInfo info:[String : Any] )
    Imgprofile.image = info[UIImagePickerControllerOriginalImage] as? UIImage
    self.dismiss(animated: true, completion: nil)
    

    制作另一个按钮,使用 Alamofire 将数据传递到 URL,并为其提供 @IBAction 出口:

输入以下数据

 @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 上传数据。

谢谢

【讨论】:

以上是关于Swift - Alamofire - 从照片库上传文件的主要内容,如果未能解决你的问题,请参考以下文章

iOS Swift + Alamofire 上传带有 exif 数据的照片

图片不会在我的 Alamofire .post 请求中发送到我的后端 - Swift 3

如何在 swift 4 中从图像选择器获取照片本地 url

使用 Swift 将照片上传到网络服务

Alamofire.uploadmultipartFormData 与 JSONEncoding.default (Swift)

使用 Alamofire 4 swift 3 上传图像