1
votes

I am using UIImageView+AFNetworking to get the image URL and using that Image to resize if the size is big. and if the size is big I need to return the height of the image in the

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

The problem is the height is returned before the block is executed and resize is returned. I want to wait till the the process which I am carrying out in the success block. to complete and then return the image size in the method.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
 UIImageView *ivImage = [[UIImageView alloc]init];
            __block BOOL isResized = NO;
            [ivImage setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[[marrMainNewsFeed objectAtIndex:indexPath.row] valueForKey:@"image"]]] placeholderImage:[UIImage imageNamed:@"placeholder.png"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                //                cell.ivImage.image = image;
                NSLog(@"image size %f",image.size.width);
                if (ivImage.image.size.width >= SCREEN_WIDTH - 10) {
                    // I resize my image by calling following method
                    ivImage.image = [CommonFunctions imageWithImage:ivImage.image scaledToMaxWidth:SCREEN_WIDTH maxHeight:0];
                    ivImage.frame = CGRectMake(ivImage.frame.origin.x, ivImage.frame.origin.y, SCREEN_WIDTH - 4, ivImage.image.size.height);
                    isResized = YES;
                    ivImage.image = image;

//                    height = ivImage.image.size.height;
                } else {
                    ivImage.image = image;
                    ivImage.contentMode = UIViewContentModeCenter;
//                    height = ivImage.image.size.height;
                }
            } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                NSLog(@"Request failed with error: %@", error);
            }];
            NSLog(@"new Height %@ for index %@",isResized?@"YES":@"NO",indexPath);
                              return height + 350;
}

The return is fired first and then the image is resized and gives the height of the image. I want to wait till the resize is completed and then return the height of the cell.

Hope I would get the fruitful resutls

1
I notice that you haven't accepted (the tick icon) any of the answers on any of the questions you've asked. If you feel they don't answer the question sufficiently, by all means comment to say so. - Airsource Ltd

1 Answers

0
votes

This is absolutely the wrong thing to do.

A URL request could take a very long time - if you did actually wait until it the image had been fetched and resized, then you could be blocking your UI for so long that you'd run the risk of the user assuming your app had crashed, and force quitting it. You'd also run an excellent chance of failing Apple testing.

If you need to know how big your image is, for layout purposes, then the only options you have are:

1) Do all of the fetching before you ever display this view

or

2) Depending on your application and server architecture, prefetch information about the layout before you fetch all the images.

The alternative and simpler course of action is to relayout once the image has been fetched.