使用标题Objective-C进行异步图像下载

时间:2014-08-07 23:57:12

标签: objective-c uiimageview uiimage nsmutableurlrequest

我试图与此库异步下载图像 https://github.com/rs/SDWebImage#using-asynchronous-image-caching-independently

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
  GalleryImageCell* cell = [collectionView  dequeueReusableCellWithReuseIdentifier:@"ImageCell" forIndexPath:indexPath];
  PSGalleriesImage* imagen = [self.Images objectAtIndex:indexPath.row];

  [cell.Image setImageWithURL:[NSURL URLWithString:imagen.link]];

  return cell;
}

图像正在下载并正常工作,但服务器正在返回占位符图像,因为我没有发送auth_token。其认为auth令牌将在标题X-PS-Auth-Token

中发送

有没有办法下载图像异步添加标题?

2 个答案:

答案 0 :(得分:0)

正如克劳迪奥所说,我只是设法改变了图书馆。在这里我发布代码:

这可以在SDWebImageDownloader.m中找到:

- (id)init
{
if ((self = [super init]))
{
    _executionOrder = SDWebImageDownloaderFIFOExecutionOrder;
    _downloadQueue = NSOperationQueue.new;
    _downloadQueue.maxConcurrentOperationCount = 2;
    _URLCallbacks = NSMutableDictionary.new;

    //CHANGES HERE
    NSMutableDictionary* headers =  [[NSMutableDictionary alloc]init];
    [headers setObject:@"image/webp,image/*;q=0.8" forKey:@"Accept"];
    [headers setObject:@"image/webp,image/*;q=0.8" forKey:@"Accept"];
    [headers setObject:[AppData GetSessionToken] forKey:@"X-PS-Auth-Token"];
    _HTTPHeaders = headers;
    //CHANGES HERE

    _barrierQueue = dispatch_queue_create("com.hackemist.SDWebImageDownloaderBarrierQueue",   DISPATCH_QUEUE_CONCURRENT);
  }
  return self;
}

答案 1 :(得分:0)

如果您不想修改库,可以将http标头设置到imageDownloader中,可能在您的applicationDidFinishLaunching上设置:

-(void)configureSDWebImage
{
    SDWebImageDownloader *webImageDownloader = [SDWebImageManager sharedManager].imageDownloader;
    [webImageDownloader setValue:@"YourHTTPValue" forHTTPHeaderField:@"YourHTTPHeader"];
}