adamtrousdale
adamtrousdale

Reputation: 415

AFNetworking setImageWithURLRequest not updating UIImageView

I am downloading an image from a URL using setImageWithURLRequest:placeholderImage:success:failure and the download of the image works correctly. The issue I am having is that when I set the downloaded image to the UIIMageView nothing shows but the placeholder is removed.

My code is as follows:

__weak UIImageView *userCardImageView;


AFImageResponseSerializer *serializer = [[AFImageResponseSerializer alloc] init];
serializer.acceptableContentTypes = [serializer.acceptableContentTypes setByAddingObject:@"image/png"];
self.userCardImageView.imageResponseSerializer = serializer;

[self.userCardImageView setImageWithURLRequest:urlRequest placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

        [userCardImageView setImage:image];

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {

        if (error)
        {
            NSLog(@"Error:   %@", error);
            NSLog(@"Error response:     %@", response);
        }

    }];

The URL is correct as when I copy and past it into a browser I get my image.

Can someone advise me how I can get my code to work to update the URL? Am I right to assign the UIImageView to a weak property? I am doing this as using self or a strong variable could lead to a retain cycle.

Upvotes: 0

Views: 1227

Answers (3)

orkenstein
orkenstein

Reputation: 2858

Should be enough just:

[self.userCardImageView setImageWithURL:url];

Upvotes: 0

James
James

Reputation: 81

It seems that you should implement the line

[userCardImageView setImage:image];    

instead of

[userCardImageView setImage:[UIImage imageWithData:userCardImageData]];

in your success block

PS: If it is the self.userCardImageView which you want to set the image in, you would rather declare a weak controller instead of a weak UIImageView reference. Something like

__weak __typeof(&*self)weakSelf = self    

Upvotes: 3

Saif
Saif

Reputation: 2958

Actually you are not assigning the downloaded image to image view correctly.[userCardImageView setImage:[UIImage imageWithData:userCardImageData]];, we need not to assign the image from this variable userCardImageData, try this code

[self.userCardImageView setImageWithURLRequest:urlRequest placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
    dispatch_async(dispatch_get_main_queue(), ^{
        [userCardImageView setImage:image]; //set image in main thread , we got downloaded image as parameter in block
    });


} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {

    if (error)
    {
        NSLog(@"Error:   %@", error);
        NSLog(@"Error response:     %@", response);
    }

}];

Hope this helps

Upvotes: 0

Related Questions