Reputation: 373
I have a UITableView that I'm trying to populate with photos from a server along other data (e.g. photo comments). The table cells on the UITableView has a UIImageView that is set with setImageWithURL:placeHolderImage:
. The problem is that only the placeholder image is being shown.
I have subclassed AFHTTPClient and I'm using Basic Authentication with my server. So, I am doing something like this:
[self setAuthorizationHeaderWithUsername:@"user" password:@"pass"];
When I do a GET request against the server, I'm getting the JSON that contains the data and the path of the images. Within the tableView:cellForRowAtIndexPath:
method, I'm trying to set the UIImageView with setImageWithURL:placeHolderImage:
, but each of the requests to download the images is returning a 401 Authorization Required.
I'm assuming that each call to the image URLs does not include the Basic Authentication info that I have in the subclassed AFHTTPClient. If that is indeed the case, is there a way to have the setImageWithURL:placeHolderImage:
use the subclassed AFHTTPClient that contains the Basic Authentication info? Or is there some other approach I can take to respect this Basic Auth in each call to download the images?
Upvotes: 1
Views: 1060
Reputation: 1163
In the constructor of your AFHTTPClient class add the following:
[self registerHTTPOperationClass:[AFImageRequestOperation class]];
Next, implement a method like this:
- (void) getImageAtPath:(NSString *)path
{
NSMutableURLRequest * request = [self requestWithMethod:@"GET" path:path parameters:nil] ;
[request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
AFHTTPRequestOperation * operation =
[self HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
[buffer setData:[operation responseData]];
NSLog( @"Got image data with length %d" , [buffer length] ) ;
// todo : use image raw data to populate image view
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog( @"unable to get data at %@ from the server %@" , path, [error localizedDescription] ) ;
}] ;
[operation start];
}
Upvotes: 0
Reputation: 63914
While the setImageWithURL:placeHolderImage:
is a super convenient method you may have to use some work arounds to get this to work as you want it too. Since image downloads don't usually require authentication this isn't too surprising. You could alter the code of UIImageView+AFNetworking.m
. This way you could use the setAuthorizationHeaderWithUsername
method to authorize yourself.
Alternatively, you could utilize the underlying methods that setImageWithURL:placeHolderImage:
uses here and create your own similar class using AFImageRequestOperation
in which you could set an appropriate authorization header. If you choose this be sure to implement similar cache methods to make everything as efficient as possible.
Upvotes: 1