【发布时间】:2016-07-22 11:53:45
【问题描述】:
我想上传一张照片。我检查了 Alamofire,但所有示例都使用 Json 和编码。我需要上传用应用程序拍摄的照片并将其作为文件发送到网络服务。 Web 服务的响应也是字符串。我怎样才能做到?
【问题讨论】:
标签: ios swift web-services post alamofire
我想上传一张照片。我检查了 Alamofire,但所有示例都使用 Json 和编码。我需要上传用应用程序拍摄的照片并将其作为文件发送到网络服务。 Web 服务的响应也是字符串。我怎样才能做到?
【问题讨论】:
标签: ios swift web-services post alamofire
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 是您的图像。
【讨论】: