cell.imageView 图像无法加载 UIImageView 类别
cell.imageView image won't load with UIImageView category
我有一个 UIImageView
类别,其中包含以下方法。问题是图像没有加载,只有在我滚动 table 并且单元格由 UITableView
重新加载之后。此外,当我打印 self.superview 时,我得到的结果为零,可能是因为 UIImageView
不只是作为子视图添加到单元格中。
- (void)imageAsynchronousRequestWithURL:(NSURL*)imageURL completion:(CompletionBlock)complete
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:imageURL];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
UIImage *image = [[UIImage alloc] initWithData:data];
self.image = image;
NSLog(@"%@", self);
NSLog(@"%@", self.superview);
NSLog(@"%@", self.superview.superview);
[self setNeedsLayout];
[self setNeedsDisplay];
if (complete) {
complete(image, error);
}
}];
}
我该如何解决?
如果 self.superview
是 nil
,那是因为您没有将此视图添加为另一个视图的子视图。为此,您必须调用:
[cell addSubview:myImageView];
也就是说,您的代码中存在许多多线程错误。如果快速滚动,图像最终会出现在错误的单元格中。如果用户将单元格滚动到屏幕之外,您也没有提供取消未完成操作的方法。
您可能只想用 SDWebImage 替换您的代码,或者至少将他们的源代码与您的进行比较。
无法在加载单元格之前将图像设置为 UITableViewCell
imageView
。唯一的方法是在 Cell
之上创建一个 subview
然后初始化这个 UIImageView
我有一个 UIImageView
类别,其中包含以下方法。问题是图像没有加载,只有在我滚动 table 并且单元格由 UITableView
重新加载之后。此外,当我打印 self.superview 时,我得到的结果为零,可能是因为 UIImageView
不只是作为子视图添加到单元格中。
- (void)imageAsynchronousRequestWithURL:(NSURL*)imageURL completion:(CompletionBlock)complete
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:imageURL];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
UIImage *image = [[UIImage alloc] initWithData:data];
self.image = image;
NSLog(@"%@", self);
NSLog(@"%@", self.superview);
NSLog(@"%@", self.superview.superview);
[self setNeedsLayout];
[self setNeedsDisplay];
if (complete) {
complete(image, error);
}
}];
}
我该如何解决?
如果 self.superview
是 nil
,那是因为您没有将此视图添加为另一个视图的子视图。为此,您必须调用:
[cell addSubview:myImageView];
也就是说,您的代码中存在许多多线程错误。如果快速滚动,图像最终会出现在错误的单元格中。如果用户将单元格滚动到屏幕之外,您也没有提供取消未完成操作的方法。
您可能只想用 SDWebImage 替换您的代码,或者至少将他们的源代码与您的进行比较。
无法在加载单元格之前将图像设置为 UITableViewCell
imageView
。唯一的方法是在 Cell
之上创建一个 subview
然后初始化这个 UIImageView