AFNetworking 后无法获取 UIImage 大小高度

Unable to get UIImage size height after AFNetworking

我一直在尝试遵循此 SO thread 但无济于事。我遇到的问题是图像没有显示,我认为这是因为当我将 heightForRowAtIndexPath 设置为 return image.size.height.

时高度为空

Feed.h

#import "AFNetworking.h"
#import <UIKit/UIKit.h>

@interface Feed : UITableViewController

@end

Feed.m

#import "Feed.h"
#import "NSDictionary+Feed.h"
#import "NSDictionary+Feed_package.h"
#import "UIImageView+AFNetworking.h"
#import "FeedItem.h"

@interface Feed ()

@property(strong) NSDictionary *json_from_url;
@property(strong) NSMutableDictionary *ImageHeightDic;

@end

static NSString * const BaseURLString = @"https://xxxx";

@implementation Feed

- (void)viewDidLoad {
    [super viewDidLoad];

    self.randomSelection = [[NSMutableDictionary alloc] init];

    NSURL *url = [NSURL URLWithString:BaseURLString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        self.json_from_url = (NSDictionary *)responseObject;
        [self.tableView reloadData];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
                                                            message:[error localizedDescription]
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];
    }];

    [operation start];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *feed_array = [self.json_from_url feed_array];
    return [feed_array count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    FeedItem *feedcell = [tableView dequeueReusableCellWithIdentifier:@"FeedItemCell"];

    if(!feedcell){

        [tableView registerNib:[UINib nibWithNibName:@"FeedItem" bundle:nil] forCellReuseIdentifier:@"FeedItemCell"];

        feedcell = [tableView dequeueReusableCellWithIdentifier:@"FeedItemCell"];

    }

    NSDictionary *daysWeather = nil;

    NSArray *feed_array = [self.json_from_url feed_array];

    daysWeather = feed_array[indexPath.row];

    feedcell.captionLabel.text = [daysWeather feed_caption];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[daysWeather image_url]]

                                             cachePolicy:NSURLRequestReturnCacheDataElseLoad

                                         timeoutInterval:60];

    UIImage *placeholderImage = [UIImage imageNamed:@"feed_image_loading"];
    [feedcell.imageLabel setImageWithURLRequest:request
                               placeholderImage:placeholderImage success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

        feedcell.imageLabel.image = image;                          
        [self.ImageHeightDic setObject:image forKey:[NSString stringWithFormat:@"%li,%li",(long)indexPath.row,(long)indexPath.section]];
                               } failure:nil];

        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:YES];
        [feedcell setNeedsLayout];

    return feedcell;

}


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

    UIImage *image = (UIImage *)[self.ImageHeightDic objectForKey:
                                 [NSString stringWithFormat:@"%li,%li",
                                  (long)indexPath.row,(long)indexPath.section]];

    return image.size.height;
    // Here is where i think the problem is because it works 
    // when i comment out the above and uncomment below

    //return 40;

}



@end
[tableView reloadRowsAtIndexPaths:@[indexPath]
                 withRowAnimation:YES];

必须从请求成功块调用。您在实际获取图像之前调用它。这意味着您的单元格大小不会更新。由于您实际上只想重新加载高度,因此您可以使用成功块中的以下内容:

dispatch_async(dispatch_get_main_queue(), ^(void){
   [tableView beginUpdates];
   [tableView endUpdates];
});

这将更新高度,但不会执行单元格的完全重新加载。 (dispatch_async用于在主线程上执行操作)。

但是,您还应该涵盖尚未加载图像和 return 有效单元格高度的情况,否则您的占位符将不会显示。