如何在 iPhone 上验证从网络下载的图像文件?

发布于 2024-07-26 06:50:04 字数 920 浏览 4 评论 0原文

我正在使用 NSURLConnection sendSynchronousRequest 方法下载图像,效果很好。 但是,有时我会遇到图像 URL 指向图像文件以外的其他内容的问题。 例如,我发现这个非图像 URL 导致了问题:http://www.100plusposters。 com/images/ExoticFlowers.jpg 该 URL 返回一个网页,我认为这是因为网站中缺少图像。

Objective-C 的一大优点是无效图像不会导致崩溃。 它简单而安静地继续运行,只是不显示任何图像,但这仍然是一个问题。

在显示之前如何验证返回的数据以确保它是有效的图像文件?

谢谢!

我的相关代码,如果有帮助的话......

NSError *error = nil;
NSURLResponse *response;
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:5];

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

if(data != nil && [error localizedDescription] == nil) 
{
    //Create UIImage object using initWithData method
    //Display UIImage
}

I am downloading images using the NSURLConnection sendSynchronousRequest method and that works fine. However, occasionally I run into an issue where the image URL points to something other than an image file. For example, I found this non-image URL was causing issues: http://www.100plusposters.com/images/ExoticFlowers.jpg The URL returns a Web page, which I assume occurs because the image is missing from the site.

One nice thing about Objective-C is that the invalid image doesn't cause a crash. It simply and quietly continues along and just doesn't display any image, but that is still a problem.

How can I validate the data returned to ensure it is a valid image file before displaying it?

Thanks!

My relevant code, in case that helps...

NSError *error = nil;
NSURLResponse *response;
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:5];

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

if(data != nil && [error localizedDescription] == nil) 
{
    //Create UIImage object using initWithData method
    //Display UIImage
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

年少掌心 2024-08-02 06:50:04

看起来 NSURLResponse 对象包含 MIMEType 属性。 这应该可以让您很好地了解返回数据的类型。

Looks like NSURLResponse object contains a MIMEType property. That should give you a pretty good idea what the type of the returned data is.

誰認得朕 2024-08-02 06:50:04

我们能否更进一步地探讨这个问题? 如果服务器上的图像数据不完整或损坏怎么办? 完整的传输工作正常,MIME 类型为 image/jpeg,UIImage 已构造且非零,但渲染器发现数据不一致,并且日志可能显示“Bad Huffman 代码”或“数据段过早结束”

我该怎么办在将 UIImage 放入视图上下文之前捕获此错误,从而在屏幕上获得不漂亮的图像?

Could we take this question one step further? What if the image data is incomplete or corrupt from the server? The complete transport works, the MIME type is image/jpeg, the UIImage is constructed and non-nil but the renderer discovers inconsistencies in the data and the log may show "Bad Huffman code" or "premature end of data segment"

How would I capture this error before tossing the UIImage into the view context and thereby getting a not-pretty image on the screen?

风尘浪孓 2024-08-02 06:50:04

我认为通过交叉检查从 Http 请求标头获得的内容长度和最终收到的数据可以告诉您下载的数据是否完整。

关于腐败我没有太多信息。

I think by crosschecking the content length obtained from the header of the Http Request and finally recieved data would give you, whether the data downloaded was complete or not.

About the corrupt i dont have much info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文