Swift 头像上传(4)Alamofire上传图片到服务器

前端之家收集整理的这篇文章主要介绍了Swift 头像上传(4)Alamofire上传图片到服务器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

接着上一个头像上传(3)的博客

项目有一个头像上传功能

下面使用Alamofire上传图片到服务器

服务器接口的参数格式是这样的:

图片需要转换为Base64字符串


  1. //发送请求上传头像的网络请求
  2. func uploadImage(){
  3. //Swift 2.0 Encoding(编码)
  4. let imagebase64String = myImageData!.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
  5. var image:String = imagebase64String
  6. let imageName = (MyConstant.UserName as String)+".png"
  7. Alamofire.request(.POST,"http://xxx/xxx/uploadImage",parameters: ["img": imagebase64String,"imgName":imageName])
  8. .responseJSON { response in
  9. print("返回上传结果")
  10. print(response.request) // original URL request
  11. print(response.response) // URL response
  12. print(response.data) // server data
  13. print("返回上传结果的json数据")
  14. print(response.result) // result of response serialization
  15. if let JSON = response.result.value {
  16. print("JSON: \(JSON)")
  17. }
  18. }
  19. }

猜你在找的Swift相关文章