从 iPhone 中的 url 加载图像,仅当图像很小时
我正在使用 NSData 的 initWithContentsOfURL 从网址加载图像。 但是,我事先不知道图像的大小,并且如果响应超过一定大小,我希望连接停止或失败。
有没有办法在 iPhone 3.0 中做到这一点?
提前致谢。
I'm using initWithContentsOfURL of NSData to load an image from a url. However, I don't know the size of the image beforehand, and I would like to the connection to stop or fail if the response exceeds a certain size.
Is there a way to do this in iPhone 3.0?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法直接通过 NSData 执行此操作,但是 NSURLConnection 将通过异步加载图像并使用 connection:didReceiveData: 检查您收到的数据量来支持这种操作。 如果超出限制,只需向 NSURLConnection 发送取消消息即可停止请求。
简单的例子:(receivedData在头部定义为NSMutableData)
You can't do it directly via NSData however NSURLConnection would support such a thing by loading the image asynchronously and using connection:didReceiveData: to check how much data you have received. If you go over your limit just send the cancel message to NSURLConnection to stop the request.
Simple example: (receivedData is defined in the header as NSMutableData)