【问题标题】:iOS: UIImageView in UITableViewCell can be changed? why I was failed?iOS:UITableViewCell 中的 UIImageView 可以更改吗?为什么我失败了?
【发布时间】:2015-05-05 13:23:11
【问题描述】:

这个问题在这里已经有了答案:

(1)How to fix UITableView separator on iOS 7?

我遇到了和iOS: UIImageView changes with change in UITableView.rowHeight, how to avoid?描述的一样的问题,当然在SO中,有很多这样的Q/As,我尝试了很多答案,但都没有得到正确的结果:

(1) 在...cellForRowAtIndexPath...:

 NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
NSString *CellLabel = [menuLabels objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = CellLabel;
tableView.rowHeight = 43;

 UIImage* im = [UIImage imageNamed: @"news"];
cell.imageView.image = im;

我得到了以下信息:

(2) 我试过上面的answer: 基于(1)的代码,我添加了:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(32,32), YES, 0);
[im drawInRect:CGRectMake(0,0,32,32)];
UIImage* im2 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.image = im2;
cell.imageView.contentMode = UIViewContentModeCenter;

我又得到了一张奇怪的照片:

追加:

我希望我得到以下结果:

图标下方是一条实线

【问题讨论】:

  • 注意:要更改行高,您必须使用 UITableViewDelegate 协议的方法 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath。
  • 行高是关键问题,我可以在storyboard 中将其改大,比如50 或更大,当然tableView.rowHeight = 43; 也可以为我工作,无论我采用哪种方式用于改变高度,我得到了相同的结果
  • 是你真正的代码还是你使用 UITableViewCell 的子类?
  • @stosha,你的意思是,也许我的图标太大了?
  • 试试这样的:tableView.rowHeight = 32.0f; UIImage* im = [UIImage imageNamed: @"news"]; cell.imageView.frame = CGRectMake(0, 0, 32, 32);单元格.imageView.image = im;而不是 (2)

标签: ios objective-c iphone uitableview uiimageview


【解决方案1】:

BeginImageContextWithOptions 的布尔设置从YES 更改为NO,我相信你奇怪的黑色背景会消失:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(32,32), NO/*Modified*/, 0);
[im drawInRect:CGRectMake(0,0,32,32)];
UIImage* im2 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

来自苹果关于UIGraphicsBeginImageContextWithOptions 的文档,NO 表明它在绘图时会保留 alpha 通道。

void UIGraphicsBeginImageContextWithOptions ( CGSize size, BOOL opaque, CGFloat scale );

不透明
指示位图是否不透明的布尔标志。如果您知道位图完全不透明,请指定 YES 以忽略 Alpha 通道并优化位图的存储。指定 NO 意味着位图必须包含一个 Alpha 通道来处理任何部分透明的像素。

【讨论】:

  • 唯一的不同是我在将YES改为NO时得到了第一个没有黑色圆垫的结果,但是谢谢!
  • @abelard20008,这不是您要的吗?如果要更改图像的大小,可以将 32 值更改为 CGSizeMake(32,32) 中所需的任何大小。
  • @Yunchen,我在我的问题中附上了一张我希望的图片,请再看一遍,谢谢
  • @abelard20008 你想说你想让图像更大并且边缘更少吗?这似乎是我从您刚刚发布的屏幕截图中可以看出的唯一区别。
  • @abelard20008,这应该是你的stackoverflow.com/questions/18773239/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-14
  • 2015-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-25
  • 2012-10-18
相关资源
最近更新 更多