AFNetWork 图片上传

    //请求 图片格式 multipart/form-data ,返回数据格式 text/plain

    class func lgyPushImage(urlString: String, parameters: [String:Any]?,array:Array<UIImage>,progress:UIProgressView?,responseBlock:((_ urlString:String?,_ isError:Bool)->Void)?)->Void{

        let manager = AFHTTPSessionManager()

        //设置相应数据支持的类型

        manager.responseSerializer = AFHTTPResponseSerializer() //支持回数据格式 text/plain

        manager.post(urlString, parameters: parameters, constructingBodyWith: { (formData) in

            var index = 0

            for item in array{

                let data = UIImagePNGRepresentation(item)

                if data != nil{

                    let dateForm = DateFormatter()

                    dateForm.dateFormat = "yyyyMMddHHmmss"

                    let namePaht = dateForm.string(from: Date.init())

                    let name = String.init(format: "%@%D.jpg", namePaht,index)

                    index += 1

                    formData.appendPart(withFileData:data!, name:"file", fileName: name, mimeType: "multipart/form-data")

                }

            }

        }, progress: { (pross) in

            

        }, success: { (dataTask, any) in

            let url = String.init(data: any as! Data, encoding: .utf8) //text/plain 格式返回数据是 Data 由于后台直接返回一条连接,这里得到的是一条连接

             responseBlock(url,false)

        }) { (dataTask, error) in

            print("error  ",error,dataTask)

            responseBlock?(error.localizedDescription, true)

       

        }

    }

你可能感兴趣的:(iOS)