1、百度搜索了一些图片----浏览器右键检查元素----资源---图像---选择想要的图片---右键选择拷贝链接地址。
2、搜集了一堆图片后取链接加入项目中、使用SDWebImage加载图片、然而不显示、寻求解决办法。
3、使用如下方法发现error报错Error Domain=NSURLErrorDomain Code=403 “The operation couldn’t be completed. (NSURLErrorDomain error 403.)”
4、由于图片链接都是http而非https、而且没有user-Agent、所以有这种问题。
5、解决方法:在使用sd_setImageWithURL方法之前,使用[SDWebImageDownloader sharedDownloader]单例设置user_Agent。设置一次之后就会被保存。
NSString *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"];
}
6、解决方法来源于:https://blog.csdn.net/wujakf/article/details/72466447