SDWebImage--http图片加载不出来的问题

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值