How can I check if the downloaded image is corrupted or not in the iOS app?

I have an iPhone app that will download photos as needed, which works great, but after a while the files get saved and only part of the image is displayed and the rest are grayed out. For example, the top 25% will be the actual image and the bottom 75% will be gray.

Here is my code for loading images, I assumed a 200 response code means the requirements were met correctly.

   ASIHTTPRequest *requestl = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:URLLarge]];
        [requestl startSynchronous];
        if ([requestl responseStatusCode]==200) {
            NSData *responseData = [requestl responseData];
            NSError *writeError = nil;
            [responseData writeToFile:savePathLarge options:NSDataWritingFileProtectionNone error:&writeError];
            if (writeError !=nil) {
                NSLog(@"\n\n Write returned error: %@ \n\n", [writeError localizedDescription]);
            }
}

      

From this question [/ questions / 1806387 / can-i-check-if-downloaded-image-is-corrupt-or-not-before-saving] it says that if I try to load this image into UIImageView and it doesn't load this the image is corrupted but my images are loading in this corrupt format no problem, so the solution doesn't work for me.

+3


source to share


1 answer


If this happens on a regular basis, you are probably having image upload problems or server issues. But still

One way is to create an MD5 hash of the image on the device and then try to compare it to the hash of the image stored on the server. If they don't match, then you know that something went wrong.



You might need your own server to do this, you can easily write a PHP script to do it for you (must be online), it would be even better if the script was on the same server as images (but you didn't mention is it your own server)

+1


source







All Articles