在iOS中停止TableView单元格UIImage的大小调整

时间:2013-09-10 20:30:56

标签: ios uiimageview resize tableview

我有一个带有文本和图像的标准(即不是自定义)TableView单元格,问题是我的图像大小不同,我想把它们放在像UIViewContentModeScaleAspectFill这样的东西中。我只是希望UIImage是54x54并且不会一直调整大小 - 我只是希望它们是正方形的!

有很多想法,但没有一个工作,我最接近工作但图像在文本的顶部(它添加了另一个图像作为子视图)。

我并不热衷于使用自定义单元格,因为我使用编辑模式来拖动内容,而我还没有想到使用自定义单元格。

修改

我最近尝试过了;

UIImage *tempImage = [UIImage imageWithData:data];
CGSize size = CGSizeMake(54, 54);
UIGraphicsBeginImageContext(size);
[tempImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.image = scaledImage;

我在cellForRow中有这个,我希望这样做;

cell.imageView.contentMode = UIViewContentModeScaleAspectFill;

这仅适用于自定义单元格;

- (void)layoutSubviews {
    [super layoutSubviews];
    self.imageView.frame = CGRectMake(0,0,54,54);
}

这很有效,但它涵盖了文字;

UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 54, 54)];
imgView.backgroundColor=[UIColor clearColor];
[imgView.layer setCornerRadius:8.0f];
[imgView.layer setMasksToBounds:YES];
[imgView setImage:[UIImage imageWithData: imageData]];
[cell.contentView addSubview:imgView];

这显示了我遇到的确切问题;

Stackoverflow problem

另一个编辑

好的,这很有效,谢谢;

[cell setIndentationWidth:54];
[cell setIndentationLevel:1];

UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 54, 54)];
imgView.backgroundColor=[UIColor clearColor];
[imgView.layer setMasksToBounds:YES];
[imgView setImage:myImages[indexPath.row]];
[cell.contentView addSubview:imgView];

1 个答案:

答案 0 :(得分:1)

我建议您继承UITableViewCell并创建自己的自定义单元格:D

但是,无论如何......尝试添加此

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

UIImageView *imgView = nil;
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

    imgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 54, 54)];
    imgView.backgroundColor=[UIColor clearColor];
    imgView.contentMode = UIViewContentModeScaleAspectFill;
    imgView.tag = 123;

    [cell.contentView insertSubview:imgView atIndex:0];
    [cell setIndentationWidth:54];
    [cell setIndentationLevel:1];
}
else
{
    imgView = (UIImageView*)[cell.contentView viewWithTag:123];
}
imgView.image = [UIImage imageNamed:@"myImage"];