UITableViewCell: 如何在Retina显示屏上为内置的imageView添加边距

4
我想在普通的UITableViewCell(分组样式)左侧的UIImageView上应用一个边距。
我唯一找到的方法(通过here)是在将UIImage附加到UIImageView之前调整UIImage的大小。如果图像比单元格小,则会居中显示;留下所需的边距作为副作用。
好吧,这个方法可行,但现在我的图像变得模糊了,因为iPhone4上100个单位的行高不是100个像素,而是200个像素。因此,我最终得到一个缩放为90x90像素的UIImage,产生一个90x90单位(180x180像素)的UIImageView图像。出现了丑陋的模糊。
因此,我的问题是:如何在imageView周围实现边距,而不会过度降低图像质量?(最好根本不要降低质量-我以后还需要保留原始文件。)
我觉得我可能漏掉了一些显而易见的东西;我真的不想为此实现自定义单元格类。
4个回答

3
感谢大家,我想出了一种“解决方案”,它不需要子类化。
我的做法仍然是对图像进行降采样,但将其降为2倍大小(例如,在问题中是180x180)。 然后,当我要从处理过的CGImage创建最终UIImage时,我使用: UIImage: +(UIImage *)imageWithCGImage:(CGImageRef)imageRef scale:(CGFloat)scale orientation:(UIImageOrientation)orientation 并将scale:设置为2。现在一切都正常了。但我仍然会创建重复的图像,只是为了让UIKit满意。

2
在您的UITableViewCell子类中实现此方法。
-(void) layoutSubviews
{
    [super layoutSubviews];
    self.imageView.frame = CGRectInset(self.imageView.frame, 5, 5);
}

或者在UITableViewCell上创建一个类别,其中包含这个属性,并且有一个额外的结构体或CGSize属性,您可以将其设置为5、5或任何您需要的值。 - Alex Zavatone

0

注意:我还没有测试过这些代码,所以UITableViewCell可能会覆盖其中一些设置,以根据其自身的内部逻辑布置其子视图。

你尝试过调整图像视图的框架吗?试试这个:

UITableViewCell * tableViewCell = [[[UITableViewCell alloc] init] autorelease];
tableViewCell.imageView.image = [UIImage imageNamed:@"table-view-image"];

CGRect imageViewFrame = tableViewCell.imageView.frame;
imageViewFrame.origin.x += 10.0f;
tableViewCell.imageView.frame = imageViewFrame;

这只会将图像视图的 x 坐标增加 10 点,而不会对其进行填充。如果您想要在两侧都进行填充,您还可以设置图像视图的 contentMode 属性为 UIViewContentModeCenter 并调整其宽度:

UITableViewCell * tableViewCell = [[[UITableViewCell alloc] init] autorelease];
tableViewCell.imageView.image = [UIImage imageNamed:@"table-view-image"];
tableViewCell.imageView.contentMode = UIViewContentModeCenter;

CGRect imageViewFrame = tableViewCell.imageView.frame;
imageViewFrame.size.width += 20.0f;
tableViewCell.imageView.frame = imageViewFrame;

这将使图像视图宽度增加20个点,但由于内容模式设置为居中,图像将被绘制而不会拉伸。如果您的图像尺寸正确,则这将在左右两侧有效地填充图像10个点。但是,您需要注意,如果这样做,您提供的UIImage必须已经是适合图像视图的确切尺寸。contentMode的默认设置是UIViewContentModeScaleToFill,因此它将自动缩放图像以填充图像视图的框架。将其设置为UIViewContentModeCenter将不再执行此操作,但它将居中实际图像。


0
在我的情况下,我使用了自己的子类继承自UITableViewCell。 这非常干净且易于使用。 此外,我可以使用不同大小的图像来调整此单元格的大小。 关键是使用其他属性来替换常规的imageView 1:在子类中,我添加了一个属性mainImageView,可以代替imageView
@property (nonatomic, retain) UIImageView *mainImageView;

2: 然后在 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 中。 我分配并初始化了 mainImageView。 您可以为 mainImageView 设置任何框架(rect)。 将其作为子视图添加到 contentView 中。 我使用 insetImageView 将其与 `contentView' 的垂直中心对齐。

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {
        // Initialization code.     
        mainImageView = [[UIImageView alloc] initWithFrame:frameCellMainImageView];
        [self.contentView addSubview:mainImageView];

                insetImageView = (sizeRowHeightTwoLinedDetail - frameCellMainImageView.size.height) / 2.0;

    }

    return self;
}

3: 重写 - (void)layoutSubviews 方法,确保其他属性如 textLabeldetailTextLabel 根据添加的属性 mainImageView 来设置它们的框架位置。

- (void)layoutSubviews {
    [super layoutSubviews];

    CGRect frameImageView = frameCellMainImageView;
    frameImageView.origin.x = insetImageView;
    frameImageView.origin.y = insetImageView;

    [self.mainImageView setFrame:frameImageView];

    // Use changed frame
    frameImageView = self.mainImageView.frame;
    CGFloat newLeftInset = frameImageView.size.width + insetImageView;

    CGRect frameTextLabel = self.textLabel.frame;
    CGRect frameDetailLabel = self.detailTextLabel.frame;

    frameTextLabel.origin.x += newLeftInset;
    frameDetailLabel.origin.x += newLeftInset;

    CGFloat newTextWidth = 320.0;
    newTextWidth -= newLeftInset;
    newTextWidth -= insetImageView;
    newTextWidth -= insetImageView;

    frameTextLabel.size.width = newTextWidth;
    frameDetailLabel.size.width = newTextWidth;

    [self.textLabel setFrame:frameTextLabel];
    [self.detailTextLabel setFrame:frameDetailLabel];
}

4:在使用此单元格作为UITableDataSourceDelegate方法时,请使用cell.mainImageView接收消息,而不是常规的cell.imageView


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接