最新做新項目皆串,發(fā)現(xiàn)圖片加載不出來。圖片url是這樣的
https://f.shiyongbao365.com/shell/weilaijishi/9f868738f6ac4be7b647f209e7d9fb6a.jpeg
瀏覽器可以加載出來印荔,當在項目中SDWebImage加載不出來查看錯誤打印
Error Domain=NSURLErrorDomain Code=-1100
網(wǎng)上有各種解決方案低葫,在這里也給大家總結(jié)出來
方案一:
[cell.diviceIV sd_setImageWithURL:[NSURL URLWithString:imgStringUrl] placeholderImage:nil options:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (image){
// Set your image over here
}else{
//something went wrong
}
}];
stackoverflow上有人的解決方式
把options 設(shè)置成SDWebImageRetryFailed 讓其能在失敗后重試
此方法不奏效
方案二:
有人說,可能是最開始沒有請求到仍律,這個url一直在圖片請求失敗列表里嘿悬,然后循環(huán)請求失敗。
于是我在請求前加了下面的代碼:
[[SDWebImageManager sharedManager].failedURLs removeObject:url];
結(jié)果依然加載不出來
方案三:
檢查url是否有中文字符水泉,需要編碼
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
我的url沒有中文字符善涨,排除
方案四:
https請求導(dǎo)致的失敗,在info.plist文件中添加屬性
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
我已經(jīng)添加了茶行,這個也可以排除
方案五:
設(shè)置用戶代理
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"];
}
結(jié)果也并沒有解決躯概,這個方案也啟發(fā)檢查圖片請求頭
方案六:
因為之前的項目,用到了局域名域名請求畔师,所以查看代碼發(fā)現(xiàn)在圖片的請求頭里面加了代碼設(shè)置host
if (kUsingTestServer) {
[_imageDownloader setValue:@"nicolas.test.com" forHTTPHeaderField:@"Host"];
}
把相關(guān)的代碼去掉娶靡,完美解決問題。
如果你使用SDWebImage加載圖片是也失敗看锉,你可以從這些方案里面去查找姿锭。
或許能夠給你啟發(fā)。
文章和代碼若有不對地方伯铣,歡迎批評指正呻此。