繁体   English   中英

在自定义单元格UITableViewController中四舍五入的Rect,同时保持宽高比

[英]Rounded Rect's in custom cell, UITableViewController while maintaining aspect ratio

当引用带有圆角的UIImage时,我能够创建具有圆角矩形的图像。 这无疑帮助了我的滚动刷新率。

这个问题我现在是,我一般用UIViewContentModeScaleAspectFill同时提请我UIImages进入视野的。

使用UIBezierPath绘制,然后将图像置于其边界内UIBezierPath图像失去其宽高比。

[UIBezierPath bezierPathWithRoundedRect:imageView.bounds 
                    cornerRadius:10.0] addClip];

[image drawInRect:imageView.bounds];

编写自定义比例尺解决方案是唯一的出路吗?

谢谢

如果可能,我建议您考虑使用图层合成,而不是尝试自己绘制整个内容。 例如:

  • 创建具有舍入内容的CALayer
  • 使用显示图像的标准方法(即UIImageView
  • 将图像层的mask属性设置为roundrect层

请参阅本文此问题的一些想法。

GSize size1 = image.size;
CGSize size2 = imageView.bounds.size;
CGRect newRect;
if (size1.width / size1.height > size2.width / size2.height) {
    CGFloat newWidth = size2.height * size1.width / size1.height;
    newRect = CGRectMake((size2.width - newWidth)/2, 0, newWidth, size2.height);
} else {
    CGFloat newHeight = size1.height / size1.width * size2.width;
    newRect = CGRectMake(0, (size2.height - newHeight)/2, size2.width, newHeight);
}
[image drawInRect:newRect];
imageView.image = UIGraphicsGetImageFromCurrentImageContext();

问题在于,您引用的方法使用图像视图的边界作为图像上下文的大小,并且它可能没有正确的宽高比,因此您需要先设置图像视图的大小,然后再传递它。 ,如果您希望所有图像视图都具有相同的宽度,请传入该宽度以及一个由该宽度和图像的纵横比确定的高度。

CGFloat高度= imageView.bounds.size.width *(image.size.width / image.size.height);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM