0

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];
Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62
davetw12
  • 1,815
  • 2
  • 20
  • 27
  • Can you post the code you are using to set the imageview? Posting an example URL wouldn't hurt either. – LJ Wilson Oct 19 '12 at 10:15
  • Please print the error in the failure blocks, rather than "NSLog(@"Log does not show");" . Then you wiil find the question exactly. – user2779354 Sep 14 '13 at 14:14

2 Answers2

1

setImageWithURL: is a UIImageView category and cannot be used for setting a UIImage only used on a UIImageView.

[imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeHolderImage"]];

Here's the documentation: http://engineering.gowalla.com/AFNetworking/Categories/UIImageView(AFNetworking).html

runmad
  • 14,846
  • 9
  • 99
  • 140
0

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.

davetw12
  • 1,815
  • 2
  • 20
  • 27