SDWebImage不显示图片的问题

公司的项目使用了第三方的新闻API,获取的图片URL在Mac的浏览器可以显示,但是使用SDWebImage在手机上就显示不出来,同时用Windows的浏览器打开错误码为403;url类型如下:
http://t10.baidu.com/it/u=4150138206,1760618553&fm=173&app=25&f=JPEG?w=554&h=369&s=FBE1A809228E4F5F88A852F403005024

使用系统方法,同样显示不出来

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];

解决办法:在加载图片前加上以下代码

 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:nil]];

你可能感兴趣的:(SDWebImage不显示图片的问题)