5

mM app downloads some new images from my Parse.com backend. Example code:

//Where object is a downloaded PFObject
PFFile *image = object[@"image"];
[image getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {

    if(!error) {
        UIImage *image = [UIImage imageWithData:data];
        //Do more work here…
    }
}

However I have noticed that if there is a problem with the connection or some kind of general error the image will be downloaded (with no error) but the image will be distorted with black jagged lines and not complete. Is there a way to check if the downloaded image is completely intact and not distorted?

Paul Cezanne
  • 8,629
  • 7
  • 59
  • 90
Kex
  • 8,023
  • 9
  • 56
  • 129
  • Probably a Parse bug – Alistra Jun 20 '15 at 13:38
  • Also wondering about this, had issues where half the image would download so the bottom half would be black. Or the image would look like it had a weird yellow filter over it. Encountering this issue on Parse 1.7.5 – Drenguin Jul 02 '15 at 01:36

1 Answers1

0

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. :)

spicydog
  • 1,644
  • 1
  • 17
  • 32
  • what about for Parse though? – Kex Jul 07 '15 at 12:11
  • I see, we have the same problem but different tools. Can you check **data** inside **if** before **imageWithData**? In that case, you can still check if the image data is corrupt or not if you know an image type. If the image is corrupt then you may re-download it again. I think I will remove the answer about checking Content-Length in header since it doesn't work here. – spicydog Jul 07 '15 at 15:23