Kex*_*Kex 5 objective-c ios parse-platform
mM app从我的Parse.com后端下载一些新图像.示例代码:
//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…
}
}
Run Code Online (Sandbox Code Playgroud)
但是我注意到,如果连接出现问题或某种一般错误,将下载图像(没有错误),但图像会因黑色锯齿线而失真而不完整.有没有办法检查下载的图像是否完整无损并且没有扭曲?
我之前也遇到过这个问题,我检查标题并将内容长度与完成下载的数据进行比较。
如果 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.
}
Run Code Online (Sandbox Code Playgroud)
您还可以将其与用于下载图像的 NSHTTPURLResponse 一起使用以保存 HTTP 请求。
接下来是获取 NSData 的长度。您可以使用方法:
NSUInteger dataLength = [downloadedData length];
Run Code Online (Sandbox Code Playgroud)
然后比较两个值,如果两个长度相等,则下载完成,否则需要重新下载。
您还可以通过读取 Content-Type 标头并检查数据的某些第一个和最后一个字节来检查图像是否已损坏。
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. :)
| 归档时间: |
|
| 查看次数: |
163 次 |
| 最近记录: |