iOS:图像视图方面适合角半径 [英] iOS: Image view aspect fit with corner radius

查看:24
本文介绍了iOS:图像视图方面适合角半径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用以下代码将内容模式的角半径设置为纵横比:

I want to set corner radius with content mode as aspect fit using following code:

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

但我只为内容模式获取输出作为方面适合.我也尝试过:

but i am getting output only for content mode as aspect fit. I have also tried for:

  cell.imgvAlbum.layer.masksToBounds  = YES;

圆角半径怎么办?请建议我一些解决方案.提前致谢.

What to do for corner radius? Please suggest me some solution. Thanks in advance.

推荐答案

使用下面的方法获得圆角指定半径的圆角图像,应用以上所有属性作为UIViewContentModeScaleAspectFit,剪辑到界限等在图像视图上调用下面的函数来设置接收到的图像.

Use below method to get a Rounded corner image with the specified radius for rounded corner, apply all your above properties as UIViewContentModeScaleAspectFit, clip to bounds e.t.c. on image view and set the received image by calling below function on the image view.

-(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:图像视图方面适合角半径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆