gpt4 book ai didi

ios - AF网络 : Send image from file

转载 作者:搜寻专家 更新时间:2023-10-31 19:39:59 24 4
gpt4 key购买 nike

我正在尝试发送包含图像的多部分发布请求。以下代码工作正常:

 manager.POST( apiUrl + "/location/add",
parameters: parameters,
constructingBodyWithBlock: { (formData : AFMultipartFormData!) -> Void in
// formData.appendPartWithFileURL(NSURL(string: location.imagePath!), name: "image", error: nil)},
formData.appendPartWithFileData(img, name: imgParam, fileName: "randomimagename.jpg", mimeType: "image/jpeg")},
success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
println("JSON: " + responseObject.description)
var dict = responseObject as NSDictionary
let json = JSONValue(dict)

var message = ""
if let msg = json["message"].string {message = msg}
var success = false
if let s = json["success"].bool {
callback(success: success, msg: message)
}
},
failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
println("Error: " + error.localizedDescription)
var apiError = ApiError()
apiError.noConnection = true
errorCallback(apiError: apiError)
})

我想使用 appendPartWithFileURL 而不是 appendPartWithFileData。如果我用上面代码中注释掉的行替换第 5 行,我会得到以下编译器错误:

Extra argument 'constructingBodyWithBlock' in call

有人知道怎么解决吗?


编辑: 找到了一个(非常非常非常奇怪的)解决方案。替换行

formData.appendPartWithFileURL(NSURL(string: location.imagePath!), name: "image", error: nil)},

var temp = formData.appendPartWithFileURL(NSURL(string: location.imagePath!), name: "image", error: nil)},

除了添加 var temp = 之外,我没有做任何更改。我不知道为什么会这样,但确实如此。似乎是一个奇怪的错误。

最佳答案

如果您还没有解决这个问题,请尝试将 location.imagePath 转换为 String。

我遇到了同样的问题,直到我在下面的代码中添加了 as String:

func uploadFile(file: NSData, url: String, formData: NSDictionary, parameters: NSDictionary, completion: AnyObject -> Void, failure: NSError -> Void) {
operationManager.requestSerializer = AFJSONRequestSerializer() as AFHTTPRequestSerializer

if operationManager.reachabilityManager.networkReachabilityStatus != .NotReachable {
operationManager.POST(url, parameters: parameters, constructingBodyWithBlock: { (data) in
data.appendPartWithFileData(file, name: formData["fileName"] as String, fileName: formData["fileName"] as String, mimeType: formData["mimeType"] as String)
}, success: { (operation, responseObject) in
completion(responseObject)
}, failure: { (operation, error) in
failure(error)
})
} else {
showReachabilityAlert()
}
}

希望对您有所帮助。

关于ios - AF网络 : Send image from file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26357420/

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