[已解决]swift实现圆形头像图片和带圆角的图片

原先的代码:

            cell.imageView?.image = UIImage(named:"hdImg_48x48.jpg")

效果是:

直角的图片:

default is rectangle image

 

现在需要:

弄个圆形头像:圆形的,头像在里面的

弄个,带圆角的头像:头像还是方形的,但是四角是圆角的,不会太有棱有角的感觉

 

参考:

uitableview – TableView and image circle – Stack Overflow

var frame = cell.imageView.frame
let imageSize = 70 as CGFloat
frame.size.height = imageSize
frame.size.width  = imageSize
cell.imageView.frame = frame 
cell.imageView.layer.cornerRadius = imageSize / 2.0
cell.imageView.clipsToBounds = true

 

搜:

swift round circle image

参考:

ios – How to set image in circle in swift – Stack Overflow

Creating Circular Profile Picture and Rounded Corner Image in iOS

去试试:

(1)圆形头像,不带边框:

            cell.imageView?.image = UIImage(named:"hdImg_48x48.jpg")
           
            cell.imageView?.layer.cornerRadius = 48 / 2
            cell.imageView?.clipsToBounds = true

或:

     cell.imageView?.image = UIImage(named:"hdImg_48x48.jpg")
            
            cell.imageView?.layer.cornerRadius = (cell.imageView?.image?.size.width)! / 2
            cell.imageView?.clipsToBounds = true

效果是:

round image without border

 

(2)圆形头像,带边框:

            cell.imageView?.image = UIImage(named:"hdImg_48x48.jpg")
           
            cell.imageView?.layer.cornerRadius = 48 / 2
            cell.imageView?.clipsToBounds = true

            cell.imageView?.layer.borderWidth = 2
            cell.imageView?.layer.borderColor = UIColor.blueColor().CGColor

效果:

with border for round header image

 

(3)圆角头像:

            cell.imageView?.image = UIImage(named:"hdImg_48x48.jpg")
            
            cell.imageView?.layer.cornerRadius = 8
            cell.imageView?.clipsToBounds = true

效果是:

four corner round image

 

(4)圆角头像,带边框:

           cell.imageView?.image = UIImage(named:"hdImg_48x48.jpg")
            
            cell.imageView?.layer.cornerRadius = 8
            cell.imageView?.clipsToBounds = true

            cell.imageView?.layer.borderWidth = 2
            cell.imageView?.layer.borderColor = UIColor.blueColor().CGColor

效果是:

round corner with border



发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量