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
}