【发布时间】:2018-04-17 12:29:47
【问题描述】:
我正在使用 Swift4,我已使用此代码将图像上传到服务器
class func uploadMultipleAdvertisementImage(photos: UIImage, completion: @escaping (_ error: Error?, _ sucess: Bool, _ image_id: Int)-> Void) {
let url = URLs.uploadImages
var images = [Data]()
Alamofire.upload(multipartFormData: { (form: MultipartFormData) in
if let data = UIImageJPEGRepresentation(photos , 0.5) {
form.append(data, withName: "images", fileName: "photo.jpeg", mimeType: "image/jpeg")
}
}, usingThreshold: SessionManager.multipartFormDataEncodingMemoryThreshold, to: url, method: .post, headers: nil) { (result: SessionManager.MultipartFormDataEncodingResult) in
switch result {
case .failure(let error):
print(error)
completion(error, false, 0)
case .success(request: let upload, streamingFromDisk: _, streamFileURL: _):
upload.uploadProgress(closure: { (progress: Progress) in
print(progress)
})
.responseJSON(completionHandler: { (response: DataResponse<Any>) in
switch response.result
{
case .failure(let error):
print(error)
completion(error, false, 0)
case .success(let value):
let json = JSON(value)
print(json)
if(json["msg"] == "image uploaded successfully") {
let image_id = json["image_id"].int ?? 0
print("hiiiiiiiiii", image_id)
completion(nil, true, image_id)
}
}
})
}
}
}
成功了, 但我想将图像数组上传到名为“图像”的服务器。 有什么方法可以将图像数组上传到服务器而不是上传一个?
【问题讨论】:
-
你可以查看这个答案。 stackoverflow.com/questions/45685210/…
-
您的 api 必须以数组的形式获取图像数据。如果图像的设置参数为一个并且用新图像覆盖每个图像来自数组。联系您的后端开发人员以管理图像数组
-
@NitinGohel 没有 api 管理将图像数据作为数组获取。
标签: ios swift alamofire swift4