swift3.0使用AFNetWorking上传头像

前端之家收集整理的这篇文章主要介绍了swift3.0使用AFNetWorking上传头像前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

//上传头像

func requestUploadHeadImg(imageData:Data)

{

let img =UIImage(named:"1.jpg")

letimageData =UIImageJPEGRepresentation(img!,0.5)

let url ="上传头像的URL"


let array = ["text/html","text/plain","text/json","application/json","text/javascript"]

let sets=NSSet(array: array)as!Set<AnyHashable>

manager.responseSerializer =AFJSONResponseSerializer()

manager.responseSerializer.acceptableContentTypes = sets

manager.requestSerializer = AFHTTPRequestSerializer()



//所有需要传递的参数都使用类似于下面token这种方法

let tokenStr ="token=1234567"

let tokenData = tokenStr.data(using: .utf8)


manager.post(url,parameters:nil,constructingBodyWith: { (formData:AFMultipartFormData?)in


formData?.appendPart(withFileData: imageData,name:"file",fileName:"file.jpg",mimeType: "image/jpeg")

formData?.appendPart(withForm: tokenData,name:"token")

},success: { (oper,data) -> Voidin

let data =SuccessData.initData()

self.delegate?.requestUploadHeadImgSucc!(success: data)

}) { (opeation,error) -> Voidin

let data =ErrorData.initWithError(obj: error)

self.delegate?.requestUploadHeadImgFail!(error: data)

}

}

原文链接:https://www.f2er.com/swift/322985.html

猜你在找的Swift相关文章