简体   繁体   中英

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

I have an iPhone app that will download photos as needed which is working great but once in awhile the files are getting saved currupted and only part of the image will be shown with the rest showing up in gray. For example the top 25% will be the actual image and the lower 75% will be grey.

Here is my code to download the images, I assumed a response code of 200 meant that the reques was completed properly.

   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 [https://stackoverflow.com/questions/5699645/can-i-check-if-downloaded-image-is-corrupt-or-not-before-saving] question it says that if i try and load this image in a UIImageView and it doesn't load this image is corrupt but my images will load in this corrupt format without issue so that solution does not work for me.

If this is happening regularity, you've probably got issues downloading the image, or the server has issues. But still

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM