gpt4 book ai didi

ios - 如何使用 Alamofire 将多部分图像上传到服务器

转载 作者:行者123 更新时间:2023-11-28 12:01:23 25 4
gpt4 key购买 nike

下面提到的是我将多部分图像上传到服务器的方法,但是当我尝试这样做时,应用程序崩溃“意外发现 nill”

但是图像的值(value)是存在的,因为我是从照片库中选择它的。

func createCoupon(_ code: String, storeID: Int, description: String, terms: String, image: UIImage, startDate: String, endDate: String, couponDiscount: String, minimumDiscount: String, percentage: String, maximumDiscount: String){
let urlString = BaseURL + "create-coupon"
let params =
[
"code" : code,
"store_id" : storeID,
"type" : "merchant",
"description" : description,
"terms" : terms,
"start_date" : startDate,
"end_date" : endDate,
"coupon_discount" : couponDiscount,
"minimum_total" : minimumDiscount,
"percentage" : percentage,
"maximum_discount" : maximumDiscount
] as [String : Any]

let manager = Alamofire.SessionManager.default
manager.session.configuration.timeoutIntervalForRequest = 30000
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(UIImageJPEGRepresentation(image, 0.2)!, withName: "image", fileName: "swift_file.jpeg", mimeType: "image/jpeg")
for (key, value) in params {
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
}, to:urlString)
{
(result) in
switch result {
case .success(let upload, _, _):

upload.uploadProgress(closure: { (progress) in
//Print progress
})
upload.responseJSON { response in
let resJson = response.result.value
print(resJson)
NotificationCenter.default.post(name: Notification.Name(rawValue: NotifRequestSuccess.createCoupon.rawValue), object: nil, userInfo: ["data": resJson!])
}

case .failure(let encodingError):
NotificationCenter.default.post(name: Notification.Name(rawValue: NotifRequestError.createCoupon.rawValue), object: encodingError, userInfo: nil)
}
}

最佳答案

Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(UIImageJPEGRepresentation(self.photoImageView.image!, 0.5)!, withName: "photo_path", fileName: "swift_file.jpeg", mimeType: "image/jpeg")
for (key, value) in parameters {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}, to:"http://server1/upload_img.php")
{ (result) in
switch result {
case .success(let upload, _, _):

upload.uploadProgress(closure: { (Progress) in
print("Upload Progress: \(Progress.fractionCompleted)")
})

upload.responseJSON { response in
//self.delegate?.showSuccessAlert()
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
// self.showSuccesAlert()
//self.removeImage("frame", fileExtension: "txt")
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}

case .failure(let encodingError):
//self.delegate?.showFailAlert()
print(encodingError)
}

}

关于ios - 如何使用 Alamofire 将多部分图像上传到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50041287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com