我想使用以下代码将内容模式的角半径设置为纵横比:
cell.imgvAlbum.contentMode = UIViewContentModeScaleAspectFit;
cell.imgvAlbum.clipsToBounds = YES;
cell.imgvAlbum.layer.cornerRadius = 5.0f;
但我只获得内容模式的输出作为纵横比匹配。 我也尝试过:
cell.imgvAlbum.layer.masksToBounds = YES;
圆角半径怎么办? 请给我一些解决方案。提前致谢。
最佳答案
使用以下方法获取具有指定圆角半径的圆角图像,将上述所有属性应用为 UIViewContentModeScaleAspectFit
,裁剪到边界等。在 ImageView 上,并通过在 ImageView 上调用以下函数来设置接收到的图像。
-(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];
关于iOS: ImageView 方面适合角半径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30747338/