gpt4 book ai didi

ios - 使用 Alamofire 将图像上传到服务器不起作用

转载 作者:行者123 更新时间:2023-11-30 12:06:14 24 4
gpt4 key购买 nike

我想通过 API 调用将图像从图库发送到服务器。该图像必须作为参数传递。为了将图像作为参数传递,我尝试像这样获取图像的 url,但它没有给出正确的 url..

var selectedImage : UIImage = image
let imageData: NSData = UIImagePNGRepresentation(selectedImage)! as NSData
let imageStr = imageData.base64EncodedString(options:.endLineWithCarriageReturn)
imageArray.append(image)

我也尝试像这样上传图像......

        for img in imageArray {

let url = "http://myApp..com/a/images_upload"
let headers = [ "Content-Type":"application/x-www-form-urlencoded"]

let URL = try! URLRequest(url: url, method: .post, headers: headers)
let parameters =
[
"access_token": accessToken
"image": img
] as [String : Any]


let imgData = UIImageJPEGRepresentation(img, 0.2)!

Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(imgData, withName: "image",fileName: "file.jpg", mimeType: "file")
for (key, value) in parameters {
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
}, with: URL) { (result) in
switch result {
case .success(let upload, _, _):

upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})

upload.responseJSON { response in
print(response.result.value)
if let value = response.result.value {
print("IMG UPLOADED!!!")
}
}
case .failure(let encodingError):
print(“ERROR”)
}}}

但这也会崩溃。我已经遇到这个问题有一段时间了...无法弄清楚确切的解决方案是什么...希望有人可以提供帮助...:)也确实经历了很多类似的问题。但找不到解决办法,直到...

编辑:我的参数是:

let Parameters =
[
"access_token": commonVarForAccessToken,
"seller_id": idForNewOldUser,
"product_id": self.productId,
"is_default": "1",
"sequence": 1,
"image": self.localPath
] as [String : Any]

最佳答案

Almofire 与图像:-

Alamofire.upload(multipartFormData: { (multipartFormData) in
print(parameters)
if Array(parameters.keys).contains(Keys.Image) {
multipartFormData.append(UIImageJPEGRepresentation((parameters)[Keys.Image] as! UIImage, 1)!, withName: "image", fileName: "swift_file.jpeg", mimeType: "image/jpeg")
}
for (key, value) in parameters {
print(key,value)
if key != Keys.Image{
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
}
}, to:url)
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
//Print progress
})
upload.responseJSON { response in
print(response.result)
self.getValidDict(result: response.result, completion: { (dict, error) in
var dict = dict
print(dict!)
print(parameters)
if dict == nil {
dict = NSDictionary.init(dictionary:
[kResultMessageKey: error?.localizedDescription ?? "Some error has been occured",
kResultStatusKey: false])
}
Completion(true,dict![Keys.result]! as AnyObject)
})
}
case .failure(let encodingError):
//print(encodingError.description)
Completion(false,encodingError.localizedDescription as AnyObject)
break
}
}

关于ios - 使用 Alamofire 将图像上传到服务器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46642428/

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