gpt4 book ai didi

swift - 图像未使用 Alamofire 和 swift 以 base64 格式上传

转载 作者:行者123 更新时间:2023-12-03 09:24:28 25 4
gpt4 key购买 nike

我正在尝试以 base64string 格式上传图像。在这里,我使用 alamofire 和 swift 上传带有一些参数的图像。但是我的图像没有上传到服务器。请检查我的代码,让我知道我哪里做错了。我的代码是:

func postRegistrationPlayerProfile(urlExtension: String, callback: @escaping(Bool,String?) -> Void,parameters:[String:Any]) -> Void {

let fullURL = URL(string: urlExtension)!
let manager = Alamofire.SessionManager.default
manager.session.configuration.timeoutIntervalForRequest = TimeInterval(Common.sharedInstance().REQUEST_TIME_OUT)

manager.upload(multipartFormData: { (multipartFormData) in
for (key, value) in parameters {
multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
}
}, usingThreshold: UInt64.init(), to: fullURL, method: .post) { (result) in
switch result {
case .success(let upload, _, _):
upload.responseJSON { (response : DataResponse<Any>) in
let responseDataString = Common.sharedInstance().getJSONStringFromData(data: response.data! as NSData)
print(responseDataString)
if let result = response.result.value {
let dictionary = result as! NSDictionary
let status = dictionary["status"] as! String
let message = dictionary["message"] as? String
if status == "success"{
callback(true,message)
return
}
else{
callback(false,message)
return
}

}
else{
callback(false,Common.sharedInstance().FAILED_BANNERIMG_UPLOAD)
return
}
}
case .failure(let encodingError):
print("encodingError: \(encodingError)")
callback(false,Common.sharedInstance().FAILED_BANNERIMG_UPLOAD)
}
}
}


我的参数是:
        paramters.updateValue(phone!, forKey: "phone")
paramters.updateValue(state!, forKey: "state")
paramters.updateValue(city!, forKey: "city")
paramters.updateValue(zip!, forKey: "postcode")
paramters.updateValue(travel, forKey: "travel_distance")
paramters.updateValue(base64ImageString!, forKey: "profile_image")
paramters.updateValue(country!, forKey: "country")


我正在将图像转换为 base64 字符串,例如:
  if let selectedImage = chooseProfileImage.image{
if let data = selectedImage.jpegData(compressionQuality: 1){

base64ImageString = data.base64EncodedString(options: .lineLength64Characters)

}
}

最佳答案

添加

url('data:image/jpeg;base64,)

到我的 Base64 转换后的字符串对我有用:
if let selectedImage = chooseProfileImage.image{
if let data = selectedImage.jpegData(compressionQuality: 1){
let imageString = selectedImage.jpegData(compressionQuality: 1.0)?.base64EncodedString() ?? ""
base64ImageString = "url('data:image/jpeg;base64,\(imageString)')"
profileData = data
}
}

关于swift - 图像未使用 Alamofire 和 swift 以 base64 格式上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61998268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com