【发布时间】:2018-07-19 10:56:05
【问题描述】:
我正在使用Alamofire 向服务器发送请求。现在我必须使用其他参数将一组图像(多个图像)发送到服务器。我必须发送最多 4 张图片。请有人帮助我,如何解决这个任务。我检查了 StackOverflow 的解决方案,但所有解决方案都类似于将单个图像发送到服务器。但我想使用Alamofire 发送多个图像,因为我有图像数组。
这是我的代码
func clockOutFor(userId: NSNumber, projectId: NSNumber, taskId: NSNumber, latitude: CLLocationDegrees, longitude: CLLocationDegrees, deviceClockOutTime: String, actualEndTime: String, clockOutNetworkInfo: String, clockOutBatteryStatus: String, totalDistance: NSNumber, durationTime: String, customeLabel1: String, customeLabel2: String, customeLabel3: String, customeLabel4: String, customeLabel5: String, imageDataArray: NSArray, completionHandler: @escaping CompletionBlock ) -> Void
{
let parameter : Parameters = ["gs_userId":userId, "gs_taskId":taskId, "gs_project_id":projectId, "gs_actual_end":actualEndTime, "gs_actual_end_lattitude":latitude, "gs_actual_end_longitude":longitude, "gs_clockout_device_time":deviceClockOutTime, "gs_clockout_network_status":clockOutNetworkInfo, "gs_clockout_battery_status":clockOutBatteryStatus, "gs_distance":totalDistance, "gs_time_taken":durationTime, "gs_custom1_label":customeLabel1, "gs_custom1_labe2":customeLabel2, "gs_custom1_labe3":customeLabel3, "gs_custom1_labe4":customeLabel4, "gs_custom1_labe5":customeLabel5] as [String : AnyObject]
let url = "clockout-update"
let fullUrl = baseUrl?.appendingPathComponent(url)
let headers: HTTPHeaders = [
"Authorization" : "Bearer \(token!)",
"Accept": "application/json",
"Connection": "keep-alive",
"Content-type": "multipart/form-data"
]
if token != nil {
Alamofire.upload(multipartFormData: { multipartFormData in
for i in 0..<imageDataArray.count{
let imageData1 = UIImageJPEGRepresentation(imageDataArray[i] as! UIImage, 1.0)!
multipartFormData.append(imageData1, withName: "morephoto[\(i)]" , fileName: "photo" + String(i) + ".jpg", mimeType: "image/jpeg")
}
for (key, value) in parameter {
print("Key and Value = ",key, value)
if let data = (value as AnyObject).data(using: String.Encoding.utf8.rawValue) {
multipartFormData.append(data, withName: key)
}
}
},
to: fullUrl!,method:HTTPMethod.post,
headers:headers, encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload
.validate()
.responseJSON { response in
print(response.request as Any) // original URL request
print(response.response as Any) // URL response
print(response.data as Any) // server data
print("Result",response.result) // result of response serialization
print("parameters = \(parameter)")
switch response.result {
case .success(let value):
completionHandler(value as AnyObject, "No error found")
print("responseObject: \(value)")
case .failure(let responseError):
print("responseError: \(responseError)")
}
}
case .failure(let encodingError):
print("encodingError: \(encodingError)")
let errorDesc = (encodingError as NSError).localizedDescription
completionHandler(errorDesc as NSString,"Some error found")
}
})
}
}
我在这一行遇到一个错误
if let data = (value as AnyObject).data(using: String.Encoding.utf8.rawValue)
我认为它只需要字符串参数,但我同时拥有 String 和 NSNumber。所以这是我的问题,如何对值字符串和 NSNumber 进行编码。请有人帮助/建议我。
【问题讨论】:
-
您可以发布您尝试过的代码示例吗?
-
对多个图像使用github.com/hyperoslo/ImagePicker,并将其作为字符串传递给参数
-
是的@DilipTiwari 我会检查
-
如果有任何帮助请求让我知道我已经在使用上面的链接并使用多个图像数组发送多个参数
标签: ios iphone alamofire swift4