quantumpotato
quantumpotato

Reputation: 9767

AFNetworking setting an UITableViewCell's imageView image with URL, imageview frame is still 0 after setting image

This is from the UIImageView+AFNetworking library.

    AFImageRequestOperation *requestOperation = [[AFImageRequestOperation alloc] initWithRequest:urlRequest];
    [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        if ([[urlRequest URL] isEqual:[[self.af_imageRequestOperation request] URL]]) {
            if (success) {
                success(operation.request, operation.response, responseObject);
            } else {
                self.image = responseObject;
                [self setNeedsLayout];
                [self setNeedsDisplay];
            }

            self.af_imageRequestOperation = nil;
        }

I added in setNeedsLayout and setNeedsDisplay. The imageview, a UITableViewCell 's imageView in this case, still has a 0,0,0,0 frame. Clicking on the cell will refresh the imageview but otherwise the image does not show up.

How do I force the UIImageView to resize?

I tried putting in

 self.image = responseObject;
                    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y,
                                                 self.image.size.width, self.image.size.height);
                    [self setNeedsDisplay];
                    [self setNeedsLayout];

based on

How to make UIImageView automatically resize to the size of the image loaded

's solution of setting the frame. I can log out a bigger frame there but it does not show up on screen.

Upvotes: 0

Views: 53

Answers (1)

Lveecode
Lveecode

Reputation: 1112

Its hard to tell without seeing the rest of your code, but I believe the problem might be in your completion handler for the request operation:

        if (success) {
            success(operation.request, operation.response, responseObject);
        } else {
            self.image = responseObject;
            [self setNeedsLayout];
            [self setNeedsDisplay];
        }  

You seem to set the image only when the request failed. Moving it to the success block might fix it.

        if (success) {
            success(operation.request, operation.response, responseObject);
            self.image = responseObject;
            [self setNeedsLayout];
            [self setNeedsDisplay];
        } 

Is there a particular reason why you are not using AFNetworking/UIImageView+AFNetworking.h methods? Seems like they would fit your use-case.

- (void)setImageWithURL:(NSURL *)url
       placeholderImage:(nullable UIImage *)placeholderImage;  

- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest
          placeholderImage:(nullable UIImage *)placeholderImage
                   success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image))success
                   failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure;  

Upvotes: 2

Related Questions