【问题标题】:UITableViewCell not showing image downloaded from serverUITableViewCell 不显示从服务器下载的图像
【发布时间】:2014-07-21 15:38:11
【问题描述】:

我有一个表格,它是“搜索栏和搜索显示控制器”的副产品,它的 UITableViewCell 是由 xib 定制的。下面是 Cell 的代码。我无法发现图像未显示的原因。有人可以帮我解决问题吗?除了 imageView 的内容之外,所有其他数据都显示出来了。

.h

#import <UIKit/UIKit.h>

@interface BCDDogSearchTableViewCell : UITableViewCell

@property (strong, nonatomic) IBOutlet UIImageView *dogImageView;
@property (strong, nonatomic) IBOutlet UITextView *dogDescriptionView;
@property(strong,nonatomic) NSString *imageURL;


@end

.m

#import "BCDDogTableViewCell.h"
#import "FICImageCache.h"

@interface BCDDogTableViewCell()

@property(strong,nonatomic) UIImage *image;

@end

@implementation BCDDogTableViewCell


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

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

    // Configure the view for the selected state
}

-(void)setImageURL:(NSString *)imageURL
{
    _imageURL=imageURL;
    [self startDownloadingImage];
}

-(void)startDownloadingImage
{
    self.image=nil;
    if (self.imageURL) {
        NSLog(@"The imageURL is %@",self.imageURL);

        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.imageURL]];
        NSURLSessionConfiguration *config=[NSURLSessionConfiguration ephemeralSessionConfiguration];
        NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
        NSURLSessionDownloadTask *task =
            [session downloadTaskWithRequest:request
                           completionHandler:^(NSURL *localfile, NSURLResponse *response, NSError *error) {
                               if (!error) {
                                   if ([request.URL isEqual:self.imageURL]) {//check in case things have changed somehow
                                       UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:localfile]];
                                       dispatch_async(dispatch_get_main_queue(), ^{self.image=image;});//ui thread.
                                   }
                               }else NSLog(@"loading image error: %@",error);
                           }];
        [task resume];
    }

}

-(void)setImage:(UIImage *)image
{
    NSLog(@"set image to the image view");
    self.dogImageView.image=image;
}

-(UIImage *)image
{
     NSLog(@"get image from imageView");
    return self.dogImageView.image;
}

-(UIImageView *) dogImageView
{
    if (!_dogImageView) _dogImageView=[[UIImageView alloc]init];
    return _dogImageView;
}


@end

更新

我在以下 sn-p 中添加了更多日志记录

if (!error) {
  NSLog(@"Completion block before if");
  if ([request.URL isEqual:self.imageURL]) {//check in case things have changed somehow
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:localfile]];
    dispatch_async(dispatch_get_main_queue(), ^{self.image=image;});//ui thread.
  }else{
    NSLog(@"request url %@",request.URL);
    NSLog(@"self url %@", self.imageURL);
  }
}else NSLog(@"loading image error: %@",error);

根据日志记录,if ([request.URL isEqual:self.imageURL]) 一直返回 false,尽管 else 子句中打印的两个 url 在内容上是相等的。

所以我删除了如果检查并突然显示图像。但我担心它只是在工作,因为我的桌子上有一行。那么有谁知道为什么检查失败,而里面的 url 显然是相同的?

【问题讨论】:

  • 你不应该拥有最后一个方法 dogImageView。 dogImageView 是一个 IBOutlet,你不应该在代码中创建一个。
  • @rdelmar 我删除了它,但问题仍然存在。
  • 你的日志在 setImage 里面吗?如果您在该方法中记录图像,它是 nil 吗?
  • 你在调用 -(void)setImageURL:(NSString *)imageURL in -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 吗?

标签: ios objective-c uitableview uiimageview uiimage


【解决方案1】:

我发现了问题,在我的 if 语句中,我将 url 与字符串进行比较。

if ([request.URL isEqual:self.imageURL])

感谢大家的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    相关资源
    最近更新 更多