2016-07-22 107 views
0

我想上傳照片。我檢查Alamofire,但所有的例子都與Json和編碼。我需要上傳與應用拍攝的照片並將其作爲文件發送給webservice。 Web服務的響應也是字符串。我怎麼做到的?使用Swift將照片上傳到網絡服務

回答

0
Alamofire.upload(.POST, urlString, multipartFormData: { 
     multipartFormData in 
     if let _image = self.profilePic.image { 
      if let imageData = UIImagePNGRepresentation(_image) { 
       multipartFormData.appendBodyPart(data: imageData, name: "user_image", fileName: "file.png", mimeType: "image/png") 
      } 
     } 
     for (key, value) in userInfo { 
      multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key) 
     } 
     }, encodingCompletion: { encodingResult in 
      switch encodingResult { 
      case .Success(let upload, _, _): 
       upload.responseJSON { response in 
        debugPrint(response) 
       } 
      case .Failure(let encodingError): 
       print(encodingError) 
      } 
     } 
    ) 

其中self.profilePic.image是您的圖片。

相關問題