我以前也有这个问题,我检查一个标题,并将Content-Length与完成下载的数据进行比较。
如果Web服务器正常工作并且可以返回Content-Length的正确响应,您可以使用它来检查数据。
这里是下载请求只有头一个片段:
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.
}
你也可以NSHTTPURLResponse使用它,你用它来下载图像,以节省HTTP请求。
接下来是获取NSData的长度。您可以使用方法:
NSUInteger dataLength = [downloadedData length];
然后比较这两个值,如果两个长度相等,其下载完成,否则你就需要重新下载。
您还可以通过读取Content-Type标题并检查数据的第一个字节和最后一个字节来检查图像是否已损坏。
对于PNG How to check if downloaded PNG image is corrupt? 不要忘记将“字节”转换为“char”,无论如何你都会看到警告。
对于JPEG Catching error: Corrupt JPEG data: premature end of data segment
希望这可以帮助你。 :)
可能是一个解析错误 – Alistra
也想知道这个问题,有一半的图像会下载,所以下半部分会是黑色的问题。或者图像看起来像是有一个奇怪的黄色过滤器。在Parse 1.7.5上遇到这个问题 – Drenguin