iOS - YYWebImage, SDWebImage, AVPlayer设置请求头

开发中可能添加请求头网络图片请求, 例如: 添加Referer

SDWebImage

 

// 请求头的 key
NSString *key = @"Referer";
// 请求头的 value
NSString *value = @"https://vdapi54.daoxiaomian123.cn/";

// 设置请求头
SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
[downloader setValue:value forHTTPHeaderField:key];

// 需要请求头的图片
NSString *imageStr = @"http://sptest.ohtorichina.com.cn/upload/vod/2017-12-13/201712131513155070.jpg";
UIImageView *imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(100, 100, 200, 200);
imageView.backgroundColor = [UIColor redColor];
[self.view addSubview:imageView];
[imageView sd_setImageWithURL:[NSURL URLWithString:imageStr]];

// 不需要请求头的图片
NSString *str = @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1568030373084&di=e132e34de28b5031d8c8abbd2e4a1d0f&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201502%2F02%2F20150202125117_SdVsk.jpeg";
UIImageView *imgView1 = [[UIImageView alloc] init];
imgView1.frame = CGRectMake(100, CGRectGetMaxY(imageView.frame) + 10, 200, 200);
imgView1.backgroundColor = [UIColor redColor];
[self.view addSubview:imgView1];
[imgView1 sd_setImageWithURL:[NSURL URLWithString:str]];

iOS - YYWebImage, SDWebImage, AVPlayer设置请求头_第1张图片
需要请求头的图片.png

iOS - YYWebImage, SDWebImage, AVPlayer设置请求头_第2张图片

不需要请求头的图片.png


YYWebImage

 

// 请求头的 key
NSString *key = @"Referer";
// 请求头的 value
NSString *value = @"https://vdapi54.daoxiaomian123.cn/";

// 设置请求头
YYWebImageManager *imageManage = [YYWebImageManager sharedManager];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithDictionary:imageManage.headers];
dictionary[@"Referer"] = refererStr;
imageManage.headers = dictionary;

// 需要请求头的图片
NSString *imageStr = @"http://sptest.ohtorichina.com.cn/upload/vod/2017-12-13/201712131513155070.jpg";
YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] init];
imageView.frame = CGRectMake(100, 100, 200, 200);
imageView.backgroundColor = [UIColor redColor];
[self.view addSubview:imageView];
[imageView yy_setImageWithURL:[NSURL URLWithString:str] placeholder:nil];

// 不需要请求头的图片
NSString *str = @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1568030373084&di=e132e34de28b5031d8c8abbd2e4a1d0f&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201502%2F02%2F20150202125117_SdVsk.jpeg";
YYAnimatedImageView *imgView2 = [[YYAnimatedImageView alloc] init];
imgView2.frame = CGRectMake(100, 100, 200, 200);
imgView2.backgroundColor = [UIColor redColor];
[self.view addSubview:imgView2];
[imgView2 yy_setImageWithURL:[NSURL URLWithString:str] placeholder:nil];

需要请求头的图片.png

iOS - YYWebImage, SDWebImage, AVPlayer设置请求头_第3张图片

不需要请求头的图片.png

iOS - YYWebImage, SDWebImage, AVPlayer设置请求头_第4张图片


AVPlayer

 

#import 

@interface AvplayerVc ()
/** 播放器 */
@property (nonatomic, strong) AVPlayer *player;
/** 图层 */
@property (nonatomic, strong) AVPlayerLayer *playerLayer;
@end

// 设置header
NSDictionary *header = @{@"Referer":@"https://vdapi54.daoxiaomian123.cn/"};
NSURL *url = [NSURL URLWithString:@"http://sksk2.afadd.cn/upload/preview/waichu/waichu.m3u8"];
AVURLAsset *urlAssert = [AVURLAsset URLAssetWithURL:url options:@{@"AVURLAssetHTTPHeaderFieldsKey" : header}];
AVPlayerItem *playItem = [AVPlayerItem playerItemWithAsset:urlAssert];
self.player = [[AVPlayer alloc] initWithPlayerItem:playItem];
// 图层
self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
self.playerLayer.videoGravity     = AVLayerVideoGravityResizeAspect;
self.playerLayer.frame = self.view.bounds;
[self.view.layer addSublayer:self.playerLayer];
// 播放
[self.player play];



 

你可能感兴趣的:(一天一读,新知识点)