如何验证我在iPhone上从Web下载的图像文件? [英] How can I validate an image file I download from the Web on iPhone?

查看:166
本文介绍了如何验证我在iPhone上从Web下载的图像文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NSURLConnection sendSynchronousRequest方法下载图像,并且工作正常。但是,偶尔会遇到图像URL指向图像文件以外的问题的问题。例如,我发现这个非图片网址导致了问题: http://www.100plusposters。 com / images / ExoticFlowers.jpg URL返回一个网页,我认为是因为网站中缺少图像而发生的。

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.

关于这个网页的一个好处Objective-C是无效图像不会导致崩溃。它简单而安静地继续并且不显示任何图像,但这仍然是一个问题。

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?

谢谢!

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

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
}


推荐答案

看起来像NSURLResponse对象包含a 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.

这篇关于如何验证我在iPhone上从Web下载的图像文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆