简体   繁体   English

如何在iOS应用中检查下载的图像是否已损坏?

[英]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. 我有一个iPhone应用程序,将根据需要下载照片工作得很好但偶尔文件被保存被破坏,只有部分图像将显示,其余部分显示为灰色。 For example the top 25% will be the actual image and the lower 75% will be grey. 例如,前25%将是实际图像,而较低的75%将是灰色。

Here is my code to download the images, I assumed a response code of 200 meant that the reques was completed properly. 这是我下载图像的代码,我假设响应代码为200意味着请求已正确完成。

   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. 从这个[https://stackoverflow.com/questions/5699645/can-i-check-if-downloaded-image-is-corrupt-or-not-before-saving]问题,它说,如果我尝试加载此图像在UIImageView中并且它不加载此图像已损坏但我的图像将以这种损坏的格式加载而没有问题,因此该解决方案对我不起作用。

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. 一种方法是在设备上生成图像的MD5哈希值,然后尝试将其与存储在服务器上的图像的哈希值进行比较。 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) 您可能需要自己的服务器来执行此操作,您可以轻松编写PHP脚本来为您执行此操作(应该是在线指南),如果脚本与图像位于同一服务器上会更好(但您没有如果它是你自己的服务器,请提一下)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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