2016-08-24 63 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

相關問題