我想上传照片。我检查了Alamofire,但是所有的例子都是用Json编码的。我需要上传用app拍摄的照片并将其作为文件发送到webservice。 Web服务的响应也是字符串。我该怎么做?
答案 0 :(得分: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
是你的形象。