【问题标题】:UITableViewCell ImageView Resize - Selected Row Changes Image SizeUITableViewCell ImageView Resize - 选定行更改图像大小
【发布时间】:2015-04-16 14:03:43
【问题描述】:

我正在通过如下 URL 设置表格单元格 ImageView:

NSURL *url = [[NSURL alloc] initWithString:[account valueForKeyPath:@"Avatar"]];
[cell.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"default.png"]];

然后我使用表格单元子类并调整图像大小并将其变为圆形,如下所示:

- (void)layoutSubviews {

    [super layoutSubviews];

    self.imageView.bounds = CGRectMake(0,0,32,32);
    self.imageView.layer.cornerRadius = 16;
    self.imageView.clipsToBounds = YES;
}

在我真正点击表格行之前,这非常有效。然后它将图像向左移动一些像素。

只有图像不是完美的正方形。示例 100x75 而不是 100x100

我一定是遗漏了一些小东西吗?这是一个显示偏移量的小图:

仅供参考,我引用了这篇文章:How do I make UITableViewCell's ImageView a fixed size even when the image is smaller

编辑

我也试过这个作为表格单元格cellForRowAtIndexPath

这似乎可行,但是,由于某种原因,加载表格视图时,在我开始滚动之前,并非所有图像都已加载

[[SDWebImageManager sharedManager] downloadImageWithURL:url options:0 progress: nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {

    // resize image code here, then set below
    cell.imageView.image = image;
}];

【问题讨论】:

  • 而不是在 layoutSubviews 方法中设置边界设置框架为 self.imageView.frame = CGRectMake(0,0,32,32);
  • 如果我设置框架,图像不再居中

标签: ios objective-c uitableview imageview sdwebimage


【解决方案1】:

就我而言,我这样做了

-(void)layoutSubviews
{
    [super layoutSubviews];
    [self layoutIfNeeded];

    self.imageView.layer.cornerRadius = (self.imageView.bounds.size.width / 2.0);
    self.imageView.layer.masksToBounds = YES;

}

我没有在运行时更改边界。希望对你有帮助

【讨论】:

  • 不幸的是,这不起作用。我还需要图像是 32x32
【解决方案2】:

仅供参考,我最终只是创建了一个自定义 UITableViewCell,其中 UIImageView 定义了宽度/高度 32x32 和自定义标签,以基本上模仿右细节视图。

【讨论】:

    【解决方案3】:

    如果所有图片的尺寸相同,您可以在设置 Cell 的图片视图后尝试添加cell.setNeedsLayout()

    MenuController.shared.fetchImage(url: item.imageURL) { (image) in
            if let image = image {
                DispatchQueue.main.async {
                    cell.imageView?.image = image
                    cell.setNeedsLayout()
                }
            }
        }
    

    【讨论】:

      【解决方案4】:

      试试这个:

      - (void)layoutSubviews {
      
      [super layoutSubviews];
      
      self.imageView.frame = CGRectMake(0,self.frame.size.width/2-16,32,32);
      self.imageView.layer.cornerRadius = 16;
      self.imageView.clipsToBounds = YES;
      

      }

      或者你可以尝试设置 autoresizingMask

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多