最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

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

Swift crifan 3654浏览 0评论

原先的代码:

            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

转载请注明:在路上 » [已解决]swift实现圆形头像图片和带圆角的图片

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.266 seconds, using 22.04MB memory