gpt4 book ai didi

ios - 在 swift ios 中使用 AFNetworking 上传图像

转载 作者:行者123 更新时间:2023-11-30 13:55:40 25 4
gpt4 key购买 nike

我正在尝试使用 AFNetworking 将图像上传到我的服务器在 swift 中,我收到此错误 The data couldn’t be read because it isn’t in the correct format.

  let manager = AFHTTPRequestOperationManager()
let url = "http://path/to/server"
let URL : NSURL = NSURL(string: url)!
let req : NSURLRequest = NSURLRequest(URL: URL)
let fileURL = NSURL(string: "file:///var/mobile/Media/DCIM/102APPLE/IMG_2623.PNG")
manager.POST( url, parameters: nil,
constructingBodyWithBlock: { (data: AFMultipartFormData!) in
do{
_ = try data.appendPartWithFileURL(fileURL!, name: "uploaded_file", fileName: "image.png", mimeType: "image/png")
}catch{
}
},
success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in
print("\(responseObject)")
},
failure: { (operation: AFHTTPRequestOperation!, error: NSError!) in
print("\(error.localizedDescription)")
})

有什么帮助吗?!

最佳答案

我猜你不能使用这样的文件的任意路径。尝试首先使用应用程序包中的图像文件,然后使用 NSBundle URLForResourceWithExtension 等调用来获取文件。

如果您想从用户的相机胶卷加载文件,您需要使用适当的 API,而不是直接文件路径。 (直接访问文件系统受到“应用沙箱”的严格限制。)

关于ios - 在 swift ios 中使用 AFNetworking 上传图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33652599/

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