Search code examples
objective-cuiimageviewuiimagensmutableurlrequest

Async image download with header Objective-C


I'm trying to download an image asynchronously with this library 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;
}

The image is downloading and working good, but the server is returning a placeholder image because I'm not sending the auth_token. The auth token it's suppose to be sent in the header X-PS-Auth-Token

Is there a way to download an image async adding a header?


Solution

  • As Claudio said, i just manage to change the library a it did the trick. Here i post the code:

    this goes in the 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;
    }