我爱编程

2018-05-24SDWebImage 加载图片不显示问题 -

2018-05-24  本文已影响22人  大也

今天碰到一个BUG

  1. [self.headImage sd_setImageWithURL:[NSURL URLWithString:_model.EAImg] placeholderImage:[UIImage imageNamed:@"gd"]];

多么正常的异步加载图片,他就不正常的显示了,只显示placeholderImage,其他页面倒是可以正常显示,区别就是图片的URL;这个URL是http的,别的是HTTPS;查了一些资料,说是URL问题;解决方法也有;

  1. [self.headImage sd_setImageWithURL:[NSURL URLWithString:_model.EAImg] placeholderImage:[UIImage imageNamed:@"gd"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
  2. //在这里输出下error
  3. }];

error:

一、Error Domain=NSURLErrorDomain Code=403 “The operation couldn’t be completed. (NSURLErrorDomain error 403.)”

  1. //SDWebImage 一个难以想象的BUG

  2. NSString *userAgent = @"";

  3. 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]];

  4. if (userAgent) {

  5. if (![userAgent canBeConvertedToEncoding:NSASCIIStringEncoding]) {

  6. NSMutableString *mutableUserAgent = [userAgent mutableCopy];

  7. if (CFStringTransform((__bridge CFMutableStringRef)(mutableUserAgent), NULL, (__bridge CFStringRef)@"Any-Latin; Latin-ASCII; [:^ASCII:] Remove", false)) {

  8. userAgent = mutableUserAgent;

  9. }

  10. }

  11. [[SDWebImageDownloader sharedDownloader] setValue:userAgent forHTTPHeaderField:@"User-Agent"];

  12. }

  13. [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.)”

//请求之前加上这句话

  1. [SDWebImageDownloader.sharedDownloader setValue:@"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8"
  2. forHTTPHeaderField:@"Accept"];
    原文
    https://blog.csdn.net/wujakf/article/details/72466447
上一篇 下一篇

猜你喜欢

热点阅读