在Swift iOS中使用AFNetworking上传图片

编程入门 行业动态 更新时间:2024-10-21 13:39:01
本文介绍了在Swift iOS中使用AFNetworking上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用 AFNetworking 在swift中将图像上传到服务器,但出现此错误无法读取数据,因为

I am trying to upload an image to my server using AFNetworking in swift , and I got this error The data couldn’t be read because it isn’t in the correct format.

let manager = AFHTTPRequestOperationManager() let url = "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之类的调用来获取文件。

I'm guessing that you can't use an arbitrary path to a file like that. Try using an image file from your app's bundle to start with, and use a call like NSBundle URLForResourceWithExtension to fetch the file.

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

If you want to load a file from the user's camera roll you need to use the appropriate API, not a direct file path. (Direct access to the file system is severely limited by the "app sandbox".)

更多推荐

在Swift iOS中使用AFNetworking上传图片

本文发布于:2023-11-26 02:55:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632355.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上传图片   Swift   iOS   AFNetworking

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!