iOS:图像视图纵横比与圆角半径相符
iOS: Image view aspect fit with corner radius
我想使用以下代码将内容模式的角半径设置为宽高比:
cell.imgvAlbum.contentMode = UIViewContentModeScaleAspectFit;
cell.imgvAlbum.clipsToBounds = YES;
cell.imgvAlbum.layer.cornerRadius = 5.0f;
但我得到的只是内容模式的输出,因为适合纵横比。
我也尝试过:
cell.imgvAlbum.layer.masksToBounds = YES;
圆角半径怎么办?
请给我一些解决方案。提前致谢。
使用以下方法获取具有指定圆角半径的圆角图像,将上述所有属性应用为 UIViewContentModeScaleAspectFit
,裁剪到边界 e.t.c。在图像视图上,并通过在图像视图上调用以下函数来设置接收到的图像。
-(UIImage *)makeRoundedImage:(UIImage *) image
radius: (float) radius;
{
CALayer *imageLayer = [CALayer layer];
imageLayer.frame = CGRectMake(0, 0, image.size.width, image.size.height);
imageLayer.contents = (id) image.CGImage;
imageLayer.masksToBounds = YES;
imageLayer.cornerRadius = radius;
UIGraphicsBeginImageContext(image.size);
[imageLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return roundedImage;
}
调用为
UIImage *image = [self makeRoundedImage:[UIImage imageNamed:@"accept~iphone"]
radius: 5.0f];
cell.imgvAlbum.contentMode = UIViewContentModeScaleAspectFit;
cell.imgvAlbum.clipsToBounds = YES;
cell.imgvAlbum.layer.cornerRadius = 5.0f;
cell.imgvAlbum.layer.masksToBounds = YES;
[cell.imgvAlbum setImage: image];
当使用 UIViewContentModeScaleAspectFit 时,图像并不总是填满 ImageView 的框架,这就是您看不到圆角半径的原因。
尝试将背景颜色添加到 imageView,您会发现圆角半径起作用了。
如果您想在任何情况下都看到圆角,您应该使用其他内容模式,例如 aspectFill 或 scaleToFill。
示例:
cell.imgvAlbum.contentMode = UIViewContentModeScaleAspectFill;
另一种选择是增加您放入 imageView 中的图像的大小或减小 imageView 的大小。
UIImageView Corner Radius only Top left and Right in iOS
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:imageLayer.bounds
byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)
cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
imageLayer.mask = maskLayer;
Swift 作为扩展实用程序的版本:
extension UIImage {
/**
- Parameter cornerRadius: The radius to round the image to.
- Returns: A new image with the specified `cornerRadius`.
**/
func roundedImage(cornerRadius: CGFloat) -> UIImage? {
let size = self.size
// create image layer
let imageLayer = CALayer()
imageLayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
imageLayer.contents = self.cgImage
// set radius
imageLayer.masksToBounds = true
imageLayer.cornerRadius = cornerRadius
// get rounded image
UIGraphicsBeginImageContext(size)
if let context = UIGraphicsGetCurrentContext() {
imageLayer.render(in: context)
}
let roundImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return roundImage
}
用法 - 不要忘记在 imageView
:
上重新设置新图像
switch self.imageContentMode {
case .scaleAspectFit:
// round actual image if aspect fit
self.image = self.image?.roundedImage(cornerRadius: radius)
self.imageView?.image = self.image
default:
// round image view corners
self.imageView?.roundCorners(radius: radius)
}
我想使用以下代码将内容模式的角半径设置为宽高比:
cell.imgvAlbum.contentMode = UIViewContentModeScaleAspectFit;
cell.imgvAlbum.clipsToBounds = YES;
cell.imgvAlbum.layer.cornerRadius = 5.0f;
但我得到的只是内容模式的输出,因为适合纵横比。 我也尝试过:
cell.imgvAlbum.layer.masksToBounds = YES;
圆角半径怎么办? 请给我一些解决方案。提前致谢。
使用以下方法获取具有指定圆角半径的圆角图像,将上述所有属性应用为 UIViewContentModeScaleAspectFit
,裁剪到边界 e.t.c。在图像视图上,并通过在图像视图上调用以下函数来设置接收到的图像。
-(UIImage *)makeRoundedImage:(UIImage *) image
radius: (float) radius;
{
CALayer *imageLayer = [CALayer layer];
imageLayer.frame = CGRectMake(0, 0, image.size.width, image.size.height);
imageLayer.contents = (id) image.CGImage;
imageLayer.masksToBounds = YES;
imageLayer.cornerRadius = radius;
UIGraphicsBeginImageContext(image.size);
[imageLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return roundedImage;
}
调用为
UIImage *image = [self makeRoundedImage:[UIImage imageNamed:@"accept~iphone"]
radius: 5.0f];
cell.imgvAlbum.contentMode = UIViewContentModeScaleAspectFit;
cell.imgvAlbum.clipsToBounds = YES;
cell.imgvAlbum.layer.cornerRadius = 5.0f;
cell.imgvAlbum.layer.masksToBounds = YES;
[cell.imgvAlbum setImage: image];
当使用 UIViewContentModeScaleAspectFit 时,图像并不总是填满 ImageView 的框架,这就是您看不到圆角半径的原因。
尝试将背景颜色添加到 imageView,您会发现圆角半径起作用了。
如果您想在任何情况下都看到圆角,您应该使用其他内容模式,例如 aspectFill 或 scaleToFill。 示例:
cell.imgvAlbum.contentMode = UIViewContentModeScaleAspectFill;
另一种选择是增加您放入 imageView 中的图像的大小或减小 imageView 的大小。
UIImageView Corner Radius only Top left and Right in iOS
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:imageLayer.bounds
byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)
cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
imageLayer.mask = maskLayer;
Swift 作为扩展实用程序的版本:
extension UIImage {
/**
- Parameter cornerRadius: The radius to round the image to.
- Returns: A new image with the specified `cornerRadius`.
**/
func roundedImage(cornerRadius: CGFloat) -> UIImage? {
let size = self.size
// create image layer
let imageLayer = CALayer()
imageLayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
imageLayer.contents = self.cgImage
// set radius
imageLayer.masksToBounds = true
imageLayer.cornerRadius = cornerRadius
// get rounded image
UIGraphicsBeginImageContext(size)
if let context = UIGraphicsGetCurrentContext() {
imageLayer.render(in: context)
}
let roundImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return roundImage
}
用法 - 不要忘记在 imageView
:
switch self.imageContentMode {
case .scaleAspectFit:
// round actual image if aspect fit
self.image = self.image?.roundedImage(cornerRadius: radius)
self.imageView?.image = self.image
default:
// round image view corners
self.imageView?.roundCorners(radius: radius)
}