iOS:圆角图片视图的等比缩放适应

4
我想使用以下代码将内容模式设置为aspect fit并设置圆角半径:

  cell.imgvAlbum.contentMode = UIViewContentModeScaleAspectFit;
  cell.imgvAlbum.clipsToBounds = YES;
  cell.imgvAlbum.layer.cornerRadius  = 5.0f;

但是我只能得到“内容模式”为“等比缩放”的输出。我还尝试了以下模式:

  cell.imgvAlbum.layer.masksToBounds  = YES;

如何处理圆角半径?请给我一些解决方案。先提前谢谢。


1
无论如何,您的图像视图都会获得圆角。只是使用AspectFit时,图像不会填充图像视图。要获得所需的效果,您需要根据图像的纵横比调整图像视图的大小。 - rmaddy
4个回答

4
使用以下方法获取具有指定半径的圆角图像,将所有上述属性应用于UIViewContentModeScaleAspectFit、剪切到边界等图像视图,并通过在图像视图上调用下面的函数设置接收到的图像。
-(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];

2
当使用UIViewContentModeScaleAspectFit时,图像并不总是填充ImageView的框架,这就是为什么您看不到圆角的原因。
尝试将背景颜色设置为imageView,您会发现圆角正在工作。
如果您想在任何情况下都看到圆角,则应使用其他内容模式,例如aspectFill或scaleToFill。示例:
cell.imgvAlbum.contentMode = UIViewContentModeScaleAspectFill;
另一个选项是增加放入imageView中的图像的大小或减小imageView的大小。

1
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;

1

作为扩展实用程序的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)
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接