2016-08-24 65 views
0

我试图上传图片到我的网络服务器时收到此错误。我刚刚复制并粘贴了github上的alamofire样本,我马上收到一个错误消息。我的代码如下:模糊引用会员上传alamofire

let data = UIImageJPEGRepresentation(picOutlet.image, 0.5) 


    Alamofire.upload(.POST, "phpurlhere", file: photo) 
     .progress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in 
      print(totalBytesWritten) 

      // This closure is NOT called on the main queue for performance 
      // reasons. To update your ui, dispatch to the main queue. 
      dispatch_async(dispatch_get_main_queue()) { 
       print("Total bytes written on main queue: \(totalBytesWritten)") 
      } 
     } 
     .validate() 
     .responseJSON { response in 
      debugPrint(response) 
    } 

更新:我添加JPEG表示要传递给alamofire功能,但仍然得到同样的错误。

+0

你收到了什么错误? – pableiros

+0

模糊引用成员上传(_:_:headers:file :)' – Martheli

+0

你为什么在url参数上使用'phpurlhere'而不是url? – pableiros

回答

0

的问题是,upload功能:

Alamofire.upload(.POST, "phpurlhere", file: photo) 

期待NSURL类型的对象为file:参数。你给它一个UIImage

如果你的目标是使用upload功能上传picOutlet.image,请尝试以下操作:

let data = UIImageJPEGRepresentation(picOutlet.image, 0.5) 

Alamofire.upload(.POST, "phpurlhere", data: data) 
    .progress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in 
     print(totalBytesWritten) 

     // This closure is NOT called on the main queue for performance 
     // reasons. To update your ui, dispatch to the main queue. 
     dispatch_async(dispatch_get_main_queue()) { 
      print("Total bytes written on main queue: \(totalBytesWritten)") 
     } 
    } 
    .validate() 
    .responseJSON { response in 
     debugPrint(response) 
} 
+0

我在进行更改后仍然收到相同的错误消息。 – Martheli

0

试试这个

Alamofire.upload(photoURL, to: "phpurlhere", method: .post, headers:nil) 
    .uploadProgress { progress in // main queue by default 
      progressView.progress = Float(progress.fractionCompleted) 
    } 
    .downloadProgress { progress in // main queue by default 
    print("Download Progress: \(progress.fractionCompleted)") 
    } 
    .responseJSON { response in 
    debugPrint(response) 
    } 
+0

请尽量避免将代码转储为答案,并尝试解释它的作用和原因。对于那些没有相关编码经验的人来说,你的代码可能并不明显。 – Frits

相关问题