NSMutableURLRequest:发布大量数据
我需要在 WebView
中通过 POST 发送大量数据(图像文件),因此我将 NSMutableURLRequest
与 setHTTPBody:
结合使用。
问题:如果数据大小超过 3MB,应用程序会突然开始消耗大量内存,并且所有分页都会变得缓慢。
我尝试将其更改为:
[request setHTTPBody:[NSData dataWithBytes:post.Get() length:post.Size()]];
.. 更改为:
[request setHTTPBodyStream:[NSInputStream inputStreamWithData:
[NSData dataWithBytes:post.Get() length:post.Size()]]];
.. 但似乎什么也没有发生,请求只是超时。我没有收到任何正常的回调(例如 WebViewProgressEstimateChangedNotification
),并且数据似乎没有设置。
那么,我如何(a)使 NSData 方法不占用内存或(b)使流方法起作用(假设这是一个很好的解决方法)?
I need to send large amounts of data (image files) by POST in a WebView
, so I'm using NSMutableURLRequest
with setHTTPBody:
.
Problem: if the data size is more than about 3MB, the app suddenly starts eating up huge amounts of memory and gets dog slow with all the paging.
I've tried changing this:
[request setHTTPBody:[NSData dataWithBytes:post.Get() length:post.Size()]];
..to this:
[request setHTTPBodyStream:[NSInputStream inputStreamWithData:
[NSData dataWithBytes:post.Get() length:post.Size()]]];
..but then nothing seems to happen and the request just times out. I get none of the normal callbacks (like WebViewProgressEstimateChangedNotification
) and the data doesn't seem to get set.
So, how do I (a) make the NSData
approach not be a memory hog or (b) make the stream approach work (assuming it's a good workaround)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终通过使用
NSURLConnection
手动上传,然后将结果输入WebView
来解决这个问题。作为奖励,我可以通过这种方式获得更好的上传进度更新。I ended up solving it by using
NSURLConnection
to do the upload manually, and then feeding the result into theWebView
. As a bonus, I get much better upload progress updates this way.