从 iPhone 上传文件时出现 POSIX 错误 12(“无法分配内存”)
我正在开发一个 iPhone 应用程序,该应用程序涉及将相机中的完整照片(通常每张照片在 1.5 到 2.0 MB 之间)及其缩略图(小得多)上传到 Amazon S3。
缩略图总是成功上传,但有时完整图像不会上传,当上传失败时,会失败并显示 POSIX 错误代码 12(又名 ENOMEM)。不过,我添加了调试代码来在错误发生时打印可用内存量,并且总是有相当多的可用内存,通常超过 100 MB。
此外,当通过 3G 上传时,错误会更频繁地出现,而通过 wifi 上传时,错误会更少出现——这看起来很奇怪,因为请求没有下载太多,并且上传的文件已经在内存中(我也尝试过)从磁盘流式传输,没有任何改进)。
我尝试使用 NSURLConnection、Foundation CFHTTP* 函数和 ASIHTTPRequest 库上传文件,但无论如何,错误发生的频率相同。更奇怪的是,我的谷歌搜索显示,最终用户有时会从 Safari 收到错误代码 12——我还没有看到任何 iOS 开发人员提到过这一点。我正在使用继承的代码库,因此它可能有问题,但我什至不确定要寻找什么。任何见解将不胜感激!
I'm working on an iPhone application that involves uploading full photos from the camera (generally between 1.5 to 2.0 MB each) as well as their thumbnails (much smaller) to Amazon S3.
The thumbnails always successfully upload, but sometimes the full images don't, and when they fail, they fail with POSIX error code 12, aka ENOMEM. However, I've added debug code to print the amount of free memory when the error happens, and there's always quite a bit free, usually more than 100 MB.
Furthermore, the error crops up more often when the upload is happening over 3G and less when it's over wifi -- which seems strange, since the request isn't downloading much and the file being uploaded is already in memory (I've also tried streaming it from disk with no improvement).
I've tried uploading the file using NSURLConnection, the Foundation CFHTTP* functions, and the ASIHTTPRequest library, but regardless, the error happens with the same frequency. Even stranger, all my Googling has revealed is that end users sometimes get error code 12 from Safari -- I haven't seen any iOS developers mentioning it. I'm working with an inherited code base, so it's possible there's something wrong with it, but I'm not even sure what to look for. Any insight would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我能够解决这个问题的唯一方法是直接使用套接字并手动形成 HTTP 标头。所以我的上传代码目前看起来像这样:
虽然 ASIHTTPRequest 可以在这里工作,但我们决定放弃这种依赖关系,以获得性能并让一切都在我们自己的准确控制之下。您可以使用 Wireshark 工具来调试此类事情。
The only way I was able to work around this issue, is using sockets directly and forming HTTP header manually. So my uploading code currently looks like this:
Although ASIHTTPRequest could work here, we decided to walk away from such dependencies both in order to get performance and to keep everything under our own control accurately. You can use Wireshark tool in order to debug this kind of things.
解决此问题的关键是使用流上传文件。当使用 NSMutableURLRequest 时,可以使用类似于以下内容的方法来完成:
当使用 ASIHTTPRequest 时,可以通过以下方式完成流式传输文件:
The key to getting around this issue is to upload the file using a stream. When using NSMutableURLRequest, this can be accomplished using something similar to the following:
When using ASIHTTPRequest, streaming a file is accomplished with this:
已通过使用请求操作 (
NSMutableUrlConnection
) 和@autorelease{}
作为 main 函数解决了此错误。NSPOXIS 仅有时出现。
Have resolved this error with using operation for request (
NSMutableUrlConnection
) with@autorelease{}
for main function.NSPOXIS appears only sometimes.