首页新闻找找看学习计划

ios 中 sdwebimage 怎么显示 重定向 url图片?

0
悬赏园豆:5 [待解决问题]

ios 中 sdwebimage 怎么显示 重定向 url,这个url是淘宝的头像,浏览器输入后,url会变,得到头像,在ios中怎么使用??

url: http://wwc.taobaocdn.com/avatar/getAvatar.do?userNick=\\U56de\\U8f66\\U95f4&width=80&height=80&type=sns

重定向后 url: http://gw.alicdn.com/tps/i3/TB1yeWeIFXXXXX5XFXXuAZJYXXX-210-210.png_80x80.jpg

 

应该怎么处理?

ios
liu_weixing的主页 liu_weixing | 初学一级 | 园豆:3
提问于:2017-09-28 20:31
< >
分享
所有回答(1)
0

这个问题有两个解决办法我先说简单的

由于APP图片重定向的图片或许很多可以封装一个请求体

请看代码

.h和.m文件如下

1 #import <UIKit/UIKit.h>
2 
3 typedef void(^ChongDingXiangBlock)(NSURL *url, NSError *error);
4 typedef void(^ErrorBlock)(NSError *error);
5 @interface ChongDingXiangViewController : UIViewController
6 
7 + (void)initWithPath:(NSString *)urlStr completion:(ChongDingXiangBlock)completion error:(ErrorBlock)errorBlock;
8 @end
#import "ChongDingXiangViewController.h"

@interface ChongDingXiangViewController ()

@end

@implementation ChongDingXiangViewController

+ (void)initWithPath:(NSString *)urlStr completion:(ChongDingXiangBlock)completion error:(ErrorBlock)errorBlock{
    
    NSString*  urlString = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *quest = [NSMutableURLRequest requestWithURL:url];
    quest.HTTPMethod = @"GET";//设置get请求
    
    NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
    sessionConfig.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
    //    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:[NSOperationQueue currentQueue]];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:quest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;
        NSLog(@"location2 code: %ld",res.statusCode);
        NSLog(@"location2重定向之后的地址是: %@",res.URL);
        if (!data) {
            errorBlock(error);
        }else{
            completion(res.URL,error);
        }
    }];
    
    [task resume];
}

@end

接着在要显示的图片的控制器中引用头文件并调用即可.

// 设置默认头像
   
    [ChongDingXiangViewController initWithPath:@"http://wwc.taobaocdn.com/avatar/getAvatar.do?userNick=\\U56de\\U8f66\\U95f4&width=80&height=80&type=sns" completion:^(NSURL *url, NSError *error) {
            dispatch_async(dispatch_get_main_queue(), ^{
               [_userImage sd_setImageWithURL:url];
            });
    } error:^(NSError *error) {
        
    }];

第二种方法是使用NSURLSession代理方法. 个人觉得第一种方便些!

    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:[NSOperationQueue currentQueue]];

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
willPerformHTTPRedirection:(NSHTTPURLResponse *)response
        newRequest:(NSURLRequest *)request
 completionHandler:(void (^)(NSURLRequest * __nullable))completionHandler{

    NSDictionary *dic = response.allHeaderFields;
    NSLog(@"location code: %ld",response.statusCode);
    NSLog(@"location: %@",response.allHeaderFields);
    NSLog(@"重定向之后的地址是-%@",dic[@"Location"]);
    dispatch_async(dispatch_get_main_queue(), ^{
       [_userImage sd_setImageWithURL:dic[@"Location"]];
    });
    completionHandler(request);
    //  completionHandler(nil); //这个如果为nil则表示拦截跳转。
}

 

甘林梦 | 园豆:226 (菜鸟二级) | 2018-01-11 10:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册