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")

            }

        }

    }

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,952评论 19 139
  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 13,906评论 0 38
  • 英文文档,一开始我也是抗拒的,边翻译边看,也就花费了1个小时基本就阅读过了,我的英文基础其实很差。附上链接:链接:...
    lonecolonel阅读 10,014评论 3 1
  • 前段时间我去石家庄学习,妈妈让我带上厚衣服,当时我们还在穿着衬衫,真的以为没有必要,但最后还是带着了。等到了石家庄...
    宋月才河北阅读 375评论 0 1
  • 笨鸟先飞,早入林;离群索居,易迷路! “人”,象形字,表示一个懂得鞠躬、谦逊的直立动物。一撇、一捺,有交叉重合之处...
    橘子晴朗阅读 681评论 0 0