【发布时间】:2018-05-18 11:14:22
【问题描述】:
我使用 UITableViewCell 作为 UITableView 标题。我正在使用下面的代码为 UIImageView 制作圆角。
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if(section==0){
static NSString *simpleTableIdentifier = @"ContactDetailsHeaderTableViewCell";
ContactDetailsHeaderTableViewCell *cell = (ContactDetailsHeaderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ContactDetailsHeaderTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
..................
.................
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
//rounded corners
cell.profilePicImgView.clipsToBounds = YES;
UIGraphicsBeginImageContextWithOptions(cell.profilePicImgView.bounds.size, NO, 1.0);
// Add a clip before drawing anything, in the shape of an rounded rect
[[UIBezierPath bezierPathWithRoundedRect:cell.profilePicImgView.bounds
cornerRadius:60.0] addClip];
// Draw your image
[img drawInRect:cell.profilePicImgView.bounds];
// Get the image, here setting the UIImageView image
cell.profilePicImgView.image = UIGraphicsGetImageFromCurrentImageContext();
// end drawing
UIGraphicsEndImageContext();
.............
.............
}
下面是输出
UIImageView 内的图像正在拉伸,如果我不使用圆角,则图像看起来正常。我已经尝试了 StackOverflow 中的很多代码示例,但它们都没有工作。
【问题讨论】:
-
你试过将
contentMode添加到 UIImageView 吗?而且这甚至不是一个完美的圆。 -
是的,我尝试了各种 contentModes,如 AspectFit、AspectFill、ScaleToFill,但没有任何效果。
-
您只想将 ImageView 显示为一个圆圈?那你为什么要使用 UIGraphicsBeginImageContextWithOptions,你可以让 ImageView 角半径 = imageview.width * 0.5 并确保 ImageView 的比例为 1:1 并使用内容模式来充分利用它。
-
你也可以用这个给圆形
imageView.layer.cornerRadius = imageView.bounds.width/2 -
您正在根据图像视图重写图像图形上下文,这就是
.contentMode不起作用的原因。只需通过图像视图下载图像并设置.contentMode。
标签: ios objective-c iphone uitableview