iOS图片上传,swift4
在默认的post请求中:
//上传会议图片
func upLoadMeetImage(customerModel:CustomerModel, imaData: [UIImage], success:@escaping(_:Bool)->(), failure:@escaping(_:Error)->()) {
for ima in imaData {
//把uiimage转成base64
net.parameter = ["imaFile":UIImageJPEGRepresentation(ima, 0.1)!.base64EncodedString()]
net.postImage(alertShow:true, success: { (data)in
success(true)
}) { (error)in
}
}
}
调用这个方法,我用的是alamofire
var parameter : Parameters? = nil
funcpostImage(alertShow:Bool, success:@escaping((_responseObject: [String:AnyObject])->()), failure:@escaping((_error:Error) ->())) {
let myManager =SessionManager.default
//信任服务器
myManager.delegate.sessionDidReceiveChallenge= { session, challenge in
return (URLSession.AuthChallengeDisposition.useCredential,URLCredential(trust:challenge.protectionSpace.serverTrust!))
}
//菊花
let hud =MBProgressHUD.showAdded(to:UtilitiesTools.shared().getCurrentVC().view, animated:true)
hud.dimBackground=true
Alamofire.request(self.appendModule(), method:HTTPMethod.post, parameters:parameter, encoding:URLEncoding.default, headers:nil).responseJSON{ (response) in
switchresponse.result{
case.success:
if let value = response.result.valueas? [String:AnyObject] {
if value["code"]as?String=="True"{
success(value["result"]as! [String:AnyObject])
}else{
if value["msg"] !=nil&& (value["msg"]as!String).count!=0{
failure(NSError.init(domain: value["msg"]as!String, code:0, userInfo:nil))
UtilitiesTools.shared().showPromptMessage(message: value["msg"]as!String)
}else{
failure(NSError.init(domain:"请求失败", code:0, userInfo:nil))
UtilitiesTools.shared().showPromptMessage(message:"请求失败")
}
}
hud.hide(animated:true)
}
case.failure:
failure(response.error!)
UtilitiesTools.shared().showPromptMessage(message: (response.error?.localizedDescription)!)
hud.hide(animated:true)
print("failure")
}
}
}