Evgeniy Kleban
Evgeniy Kleban

Reputation: 6965

setting image using AFNetworking

For some reason i can't set an image using AFNetworking. It actually proceed block case failure. I don't know why is that happening, because image URL is perfectly correct (as i suppose).

Please, take a look at following:

NSString *string = [NSString stringWithFormat:@"http://www.jpl.nasa.gov/spaceimages/images/mediumsize/PIA17011_ip.jpg"];
    NSURL *url = [NSURL URLWithString:string];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    [cell.myCellImageView setImageWithURLRequest:request placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
        [cell.myCellImageView setImage:image];
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {

        NSInteger HTTPStatusCode = [(NSHTTPURLResponse *)response statusCode];

        // If it's other than 200, then show it on the console.
        if (HTTPStatusCode != 200) {
            NSLog(@"HTTP status code = %id", HTTPStatusCode);
        }

    }];

It print go image on imageView and that is what NSLog typyng:

HTTP status code = 0

Why i can't achieve such simple task?

Upvotes: 0

Views: 229

Answers (1)

Logan
Logan

Reputation: 53142

Use this class that's already in afnetworking:

https://github.com/AFNetworking/AFNetworking/blob/master/UIKit%2BAFNetworking/UIImageView%2BAFNetworking.h

And it'd just be:

[cell.myCellImageView setImageWithURL:url];

If you want to do something more complex with the image, you could use this method instead:

- (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;

Upvotes: 2

Related Questions