How can I check the image file that I download from the Internet on the iPhone?

I upload images using the sendSynchronousRequest NSURLConnection method and this works fine. However, sometimes I run into a problem when the image URL points to something other than the image file. For example, I found that this URL without an image is causing problems: http://www.100plusposters.com/images/ExoticFlowers.jpg The URL returns a web page that I assume because the image is missing from the site.

One nice thing about Objective-C is that an invalid image does not cause a crash. It simply and quietly continues and just does not display images, but it is still a problem.

How can I check the returned data to make sure it is a valid image file before displaying it?

Thanks!

My relevant code, in case this 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 } 
+4
source share
3 answers

It looks like the NSURLResponse object contains the MIMEType property. This should give you a pretty good idea of ​​what type of data to return.

+8
source

Can this question be taken one step further? What to do if image data is incomplete or corrupted from the server? Fully transport work, the MIME type is image / jpeg, UIImage is designed and non-zero, but the rendering detects inconsistencies in the data, and the log may show “Bad Huffman code” or “premature end of data segment”

How can I capture this error before throwing a UIImage into the view context and thereby get a not-so-beautiful screen image?

+4
source

I think cross-checking the length of the content obtained from the Http request header, and finally, the data received will provide you with whether the downloaded data was completed or not.

I do not have much information about the corrupt.

0
source

All Articles