I also having this issue before, I check a header and compare Content-Length with the data that finish downloaded.
In the case the web server work properly and can return correct response for Content-Length you can use it to check the data.
Here is a snippet to download only header of the request:
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
request.HTTPMethod = @"HEAD";
NSHTTPURLResponse *response;
[NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:nil];
if ([response respondsToSelector:@selector(allHeaderFields)]) {
NSDictionary header = [response allHeaderFields];
NSString *rawContentLength = [header objectForKey:@"Content-Length"];
NSNumber *contentLength = [NSNumber numberWithInt:[rawContentLength intValue]];
// Convert contentLength to NSUInteger and use to compare with you NSData length.
}
you can also use it with NSHTTPURLResponse you use to download image to save HTTP request.
Next is to get length of NSData. You can use method:
NSUInteger dataLength = [downloadedData length];
Then compare both value, if the two length are equal, it it download complete, else you would need to re-download.
You can also check if the image is corrupted by read the Content-Type header and check some first and last bytes of the data.
For PNG How to check if downloaded PNG image is corrupt?
Don't forget to cast "byte" to "char", you will see warning anyway.
For JPEG Catching error: Corrupt JPEG data: premature end of data segment
Hope this can help you. :)