Можете ли вы попробовать это, это поможет вам.
Создать изображение dict: -
let dictimage = NSMutableDictionary()
dictimage.setValue("pass here Image data", forKey: "pass your image parameter name")
Ваши другие параметры поля, такие как
let dictparam = NSMutableDictionary()
dictparam.setValue("carex", forKey:"nickname")
dictparam.setValue("mantap", forKey:"password")
вызвать эту функцию
class func callAPIWithMultiPart(_ url: String,
image:NSDictionary,
param: NSDictionary,
type : HTTPMethod,
controller: UIViewController,
header : [String: String]?,
callSilently : Bool = false,
successBlock: @escaping (_ responseDict: NSDictionary? , _ response: NSArray?) -> Void,
failureBlock: @escaping (_ error: Error? , _ isTimeOut: Bool) -> Void) {
if isNetworkAvailable() {
if !callSilently {
MBProgressHUD.showAdded(to: (UIApplication.shared.delegate?.window!)! , animated: true)
}
let urlWithUTF8 = url.addingPercentEncoding(withAllowedCharacters:NSCharacterSet.urlQueryAllowed)
print("**************************************************")
print("URL : \(urlWithUTF8!)")
print("Header : \(String(describing: header))")
print("Parameter For video : \(video.count)")
print("Parameter : \(param)")
Alamofire.upload(multipartFormData: { (multipartFormData) in
let arrParametersKey = NSMutableArray(array: param.allKeys)
let arrParametersValues = NSMutableArray(array: param.allValues)
//For normal Parameters
for index in 0 ..< arrParametersKey.count {
if let strKey = arrParametersKey.object(at: index) as? String , let strValue = arrParametersValues.object(at: index) as? String {
print("********************* MultiPart ******************")
print("strKey : \(strKey)")
print("strValue : \(strValue)")
multipartFormData.append(strValue.data(using: .utf8)!, withName: strKey)
}
}
//For File
if let strFileKeyForWebservices = video.value(forKey: "FileKeyForWebservices") as? String, let strFileName = video.value(forKey: "FileName") as? String, let dataForWebservices = video.value(forKey: "FileData") as? Data, let strFileMineType = video.value(forKey: "FileMineType") as? String {
print("********************* MultiPart File ******************")
print("FileKeyForWebservices : \(strFileKeyForWebservices)")
print("strFileName : \(strFileName)")
print("dataForWebservices : \(dataForWebservices.count)")
print("strFileMineType : \(strFileMineType)")
multipartFormData.append(dataForWebservices, withName: strFileKeyForWebservices, fileName: strFileName, mimeType: strFileMineType)
}
}, usingThreshold: UInt64.init(), to: urlWithUTF8!, method: type, headers: header) { (encodingResult) in
switch encodingResult {
case .success(let upload, _, _):
if !callSilently {
DispatchQueue.main.async {
MBProgressHUD.hide(for: ((UIApplication.shared.delegate?.window)!)!, animated: true)
}
}
upload.responseJSON { response in
print("Response : \(String(describing: response.response?.statusCode))");
if let aDict = response.result.value as? NSDictionary {
successBlock(aDict,nil)
} else if let aArray = response.result.value as? NSArray {
successBlock(nil,aArray)
} else {
failureBlock(nil, true)
}
}
break
case .failure(let encodingError):
if !callSilently {
DispatchQueue.main.async {
MBProgressHUD.hide(for: ((UIApplication.shared.delegate?.window)!)!, animated: true)
}
}
UIAlertController.showAlertWithOkButton(controller, aStrMessage: Localization("alert_SomethingWentWrong") , completion: nil)
failureBlock(encodingError, false)
break
}
}
} else {
// Internet is not connected
UIAlertController.showAlertWithOkButton(controller, aStrMessage: "Internet is not available", completion: nil)
let aErrorConnection = NSError(domain: "InternetNotAvailable", code: 0456, userInfo: nil)
failureBlock(aErrorConnection as Error , false)
}
}
Как пользоваться
self.callAPIWithMultiPart("here apiURL", image: dictimage, param: dictparam, type: .post, controller: controller, header: requestHeader, successBlock: successBlock, failureBlock: failureBlock)