Search code examples
iosios4uiimageviewnsoperationafnetworking

Issue with setImageWithURL AFNetworking success handler


Neither the success or failure handler appears to be called when setImageWithURLRequest: is called on my UIImage object. SetImageWithURL does not set my image either. If I use setImageWithURL: placeholder: the placeholder is set but the image from the url never gets set.

Pardon my not posting code sooner. I have tried to use two methods (with AFNetworking) to asynchronously set my image views:

[_imageView setImageWithURL:[NSURL URLWithString:@"path/to/file"]];

[_imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.example.com/path/to/image.jpg"]] placeholderImage:[UIImage imageNamed:@"your_image_here.jpg"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
NSLog(@"Your image request succeeded!");
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"Your image request failed...");
}];

I have tried having these triggered in viewDidLoad but the image view never loads.

After looking through the actual function declaration of setImageWithURLRequest: and it appears that neither the success or failure block is executed when I call it. I've placed NSLogs in the code to give you guys an idea of what happens:

- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest 
      placeholderImage:(UIImage *)placeholderImage 
               success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success
               failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure
{
NSLog(@"Log shows");

[self cancelImageRequestOperation];

NSLog(@"Log shows");
UIImage *cachedImage = [[[self class] af_sharedImageCache] cachedImageForRequest:urlRequest];

NSLog(@"Log shows");

if (cachedImage) {
NSLog(@"Log does not show");

self.image = cachedImage;
self.af_imageRequestOperation = nil;

if (success) {
    success(nil, nil, cachedImage);
    NSLog(@"Log does not show");

}
} else {
NSLog(@"Log shows");

self.image = placeholderImage;

AFImageRequestOperation *requestOperation = [[AFImageRequestOperation alloc] initWithRequest:urlRequest];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    if ([[urlRequest URL] isEqual:[[self.af_imageRequestOperation request] URL]]) {
        self.image = responseObject;
        self.af_imageRequestOperation = nil;
        NSLog(@"Log does not show");
    }

    if (success) {
        success(operation.request, operation.response, responseObject);
        NSLog(@"Log does not show");
    }

    [[[self class] af_sharedImageCache] cacheImage:responseObject forRequest:urlRequest];


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    if ([[urlRequest URL] isEqual:[[self.af_imageRequestOperation request] URL]]) {
        self.af_imageRequestOperation = nil;
        NSLog(@"Log does not show");
    }

    if (failure) {
        failure(operation.request, operation.response, error);
        NSLog(@"Log does not show");
    }

}];

self.af_imageRequestOperation = requestOperation;

[[[self class] af_sharedImageRequestOperationQueue] addOperation:self.af_imageRequestOperation];

Solution

  • Thanks for the help guys but like everyone on here noticed, there was no issue with my code. The issue was actually with regard to ARC. I had it turned off in my AFNetworking files. After turning it on, everything worked just fine.