iOS - YYWebImage, SDWebImage, AV
2019-09-29 本文已影响0人
浅宇落
开发中可能添加请求头网络图片请求, 例如: 添加
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]];
需要请求头的图片.png
不需要请求头的图片.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
不需要请求头的图片.png
AVPlayer
#import <AVFoundation/AVFoundation.h>
@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];
希望此篇文章对你们有用,如果错误请指出。