今天碰到一个BUG
[self.headImage sd_setImageWithURL:[NSURL URLWithString:_model.EAImg] placeholderImage:[UIImage imageNamed:@"gd"]];
多么正常的异步加载图片,他就不正常的显示了,只显示placeholderImage,其他页面倒是可以正常显示,区别就是图片的URL;这个URL是http的,别的是HTTPS;查了一些资料,说是URL问题;解决方法也有;
[self.headImage sd_setImageWithURL:[NSURL URLWithString:_model.EAImg] placeholderImage:[UIImage imageNamed:@"gd"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//在这里输出下error
}];
error:
一、Error Domain=NSURLErrorDomain Code=403 “The operation couldn’t be completed. (NSURLErrorDomain error 403.)”
//SDWebImage 一个难以想象的BUG
NSString *userAgent = @"";
userAgent = [NSString stringWithFormat:@"%@/%@ (%@; iOS %@; Scale/%0.2f)", [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleExecutableKey] ?: [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleIdentifierKey], [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] ?: [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleVersionKey], [[UIDevice currentDevice] model], [[UIDevice currentDevice] systemVersion], [[UIScreen mainScreen] scale]];
if (userAgent) {
if (![userAgent canBeConvertedToEncoding:NSASCIIStringEncoding]) {
NSMutableString *mutableUserAgent = [userAgent mutableCopy];
if (CFStringTransform((__bridge CFMutableStringRef)(mutableUserAgent), NULL, (__bridge CFStringRef)@"Any-Latin; Latin-ASCII; [:^ASCII:] Remove", false)) {
userAgent = mutableUserAgent;
}
}
[[SDWebImageDownloader sharedDownloader] setValue:userAgent forHTTPHeaderField:@"User-Agent"];
}
[self.headImage sd_setImageWithURL:[NSURL URLWithString:_model.EAImg] placeholderImage:[UIImage imageNamed:@"gd"]];
二、Error Domain=NSURLErrorDomain Code=406 “The operation couldn’t be completed. (NSURLErrorDomain error 406.)”
//请求之前加上这句话
[SDWebImageDownloader.sharedDownloader setValue:@"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
forHTTPHeaderField:@"Accept"];