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


需要请求头的图片.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];



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现 AVPlayer 的边下边播需要用到 AVAssetResourceLoaderDelegate 协议。这个协议提供了一些方法,可以用来加载自定义资源。以下是实现边下边播的步骤: 1. 创建 AVPlayerItem 对象并为其指定 URL。 2. 创建一个 AVAssetResourceLoader 对象,并将其设置AVPlayerItem 的 resourceLoader 属性。 3. 实现 AVAssetResourceLoaderDelegate 协议中的方法来加载自定义资源。 4. 使用 AVPlayer 播放 AVPlayerItem。 下面是一个简单的示例代码: ``` @interface CustomResourceLoader : NSObject <AVAssetResourceLoaderDelegate> @property (nonatomic, strong) NSURL *url; @end @implementation CustomResourceLoader - (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest { // 获取请求的范围 long long requestedOffset = loadingRequest.dataRequest.requestedOffset; NSUInteger requestedLength = loadingRequest.dataRequest.requestedLength; // 请求自定义的资源,将资源数据填充到 loadingRequest 中 NSData *data = [self requestDataWithOffset:requestedOffset length:requestedLength]; [loadingRequest.dataRequest respondWithData:data]; [loadingRequest finishLoading]; return YES; } - (NSData *)requestDataWithOffset:(long long)offset length:(NSUInteger)length { // 从本地文件或网络中获取资源数据 // ... return data; } @end // 创建 AVPlayerItem 对象 NSURL *url = [NSURL URLWithString:@"http://example.com/video.mp4"]; AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:url]; // 创建 CustomResourceLoader 对象,并将其设置AVPlayerItem 的 resourceLoader 属性 CustomResourceLoader *resourceLoader = [[CustomResourceLoader alloc] init]; resourceLoader.url = url; [item.asset.resourceLoader setDelegate:resourceLoader queue:dispatch_get_main_queue()]; // 使用 AVPlayer 播放 AVPlayerItem AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:item]; [player play]; ``` 在这个示例中,CustomResourceLoader 类实现了 AVAssetResourceLoaderDelegate 协议中的 shouldWaitForLoadingOfRequestedResource 方法,用来处理请求自定义资源的请求。在 requestDataWithOffset:length: 方法中,可以从本地文件或网络中获取资源数据,并返回给 AVPlayerItem。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值