ios - 在单元格的 imageView 中为图像添加边框

标签 ios uitableview

我想通过 imageView 和 imageView 层为 TVC 单元格的图像添加边框

我有适用于 cell.layer 本身的代码,但是一旦我将其更改为 cell.imageView.layer,代码就什么也不做。

我在 tableView:cellForRowAtIndexPath 中使用它,并且在 Xcode 4.6.3、iOS 6.x 中

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.imageView.image = [UIImage imageNamed:@"myImage.png"];

CALayer* layer;
//layer = cell.layer;
layer = cell.imageView.layer;
[layer setMasksToBounds:YES];
[layer setCornerRadius:10.0f];
[layer setBorderWidth:10.0f];

如果应用于 cell.layer,这将有效,但对单元格的 imageView.layer 没有任何作用。

有任何想法吗?

谢谢。

编辑:仅供引用,我正在导入 <QuartzCore/QuartzCore.h> .

编辑:虽然这对单元格有效,但那是因为单元格已经有一个定义为黑色的边框颜色。 imageView 没有,因此需要像这样设置边框颜色:
[cell.imageView.layer setBorderColor: [[UIColor whiteColor] CGColor]];  
[cell.imageView.layer setBorderColor: [[UIColor blackColor] CGColor]];  
[cell.imageView.layer setBorderColor: [[UIColor colorWithRed:.5f green:.6f blue:.3f alpha:.75f] CGColor ]];

最佳答案

另一种选择是,添加

#import <QuartzCore/QuartzCore.h>

框架和使用
cell.imageView.layer.borderWidth = 1.0f;
cell.imageView.layer.setMasksToBounds = YES;

关于ios - 在单元格的 imageView 中为图像添加边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18274565/

相关文章:

iOS:如何设置 SKSpriteNode 的程序化 alpha 掩码

ios - UITableView.sizeToFit() 只渲染第一个单元格

ios - 如何让CollectionView和TableView同时滚动?

ios - 使用自动布局将 subview 尾端与 super View 中心 X 轴对齐

ios - 在 IOS 中创建可缩放 View

ios - 使用委托(delegate)从按钮传递信息以突出显示单元格

Swift 两个表格 View 单元格获取相同的值

ios - 如何以编程方式将按钮定位到带有边距的表格单元格的最右边缘?

ios - UINavigationBar 外观 addSubview 在 iOS 8 中不起作用

objective-c - 更好的图像着色逻辑/算法