AFNetworking 后无法获取 UIImage 大小高度
Posted
技术标签:
【中文标题】AFNetworking 后无法获取 UIImage 大小高度【英文标题】:Unable to get UIImage size height after AFNetworking 【发布时间】:2015-08-30 21:44:18 【问题描述】:我一直在尝试关注此SO thread,但无济于事。我得到的问题是图像没有显示,我认为这是因为当我将 heightForRowAtIndexPath 设置为返回 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
【问题讨论】:
【参考方案1】:[tableView reloadRowsAtIndexPaths:@[indexPath]
withRowAnimation:YES];
必须从请求成功块中调用。您在实际获取图像之前调用它。这意味着您的单元格大小不会更新。由于您实际上只是想重新加载高度,因此可以使用成功块中的以下内容:
dispatch_async(dispatch_get_main_queue(), ^(void)
[tableView beginUpdates];
[tableView endUpdates];
);
这将更新高度,但不会完全重新加载单元格。
(dispatch_async
用于在主线程上执行操作)。
但是,您还应该覆盖图像尚未加载的情况并返回有效的单元格高度,否则您的占位符将不会显示。
【讨论】:
以上是关于AFNetworking 后无法获取 UIImage 大小高度的主要内容,如果未能解决你的问题,请参考以下文章
AFNetworking:无法从 AFHTTPRequestOperation 获取响应字符串
无法将令牌附加到 API 调用(基本身份验证、AFNetworking)
使用 AFNetworking 异步下载图像后,是不是可以从 UIImageView 获取图像