【问题标题】:AFNetworking: setImageWithURLRequest not setting imageAFNetworking:setImageWithURLRequest 未设置图像
【发布时间】:2013-01-21 00:50:41
【问题描述】:

我正在尝试在 tableViewCell 上使用 AFNetworking 的 setImageWithURLRequestImageView 上设置图像。如果我在方法中不包含 successfailure 块,如下所示:

[cell.thumbnailImageView setImageWithURL:url placeholderImage:nil]; 

一切正常,图像设置。不幸的是我需要积木。

这是我目前在cellForRowAtIndexPath 中拥有的内容:

if (self.images) { //check if the array of images is set, fetched from a URL

        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: [[self.images objectAtIndex:indexPath.row] objectForKey:@"tbUrl"]]];

        [cell.thumbnailImageView setImageWithURLRequest:request placeholderImage:[UIImage imageNamed:@"placeholder"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                NSLog(@"success"); //it always lands here! But nothing happens
        } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                NSLog(@"fail");
        }];

    }

【问题讨论】:

    标签: iphone ios afnetworking block


    【解决方案1】:

    来自UIImageView+AFNetworking.h

    如果指定了成功块,则由该块负责 在返回之前设置图像视图的图像。

    在你的成功块中,你需要说:

    [cell.thumbnailImageView setImage: image]
    

    【讨论】:

    • 谢谢,不知道我怎么错过了!!
    • 这会导致编译器警告 (Capturing cell strongly in this block is likely to lead to a retain cycle) ... 你知道我能做些什么吗?
    • 在 SO 中有很多关于避免使用块的保留循环的讨论,但通常一个好的解决方案(在 iOS 5+ 中)是声明块中引用的对象的“弱”版本,并且在块中使用它。在这里,它看起来像 '_weak CellClass * weakCell = cell...' 请参阅此处的讨论:stackoverflow.com/questions/14667610/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 2016-12-30
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多