最新消息:VPS服务器又从fzhost.net换回Linode了,主题仍用朋友推荐的大前端D8

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

Swift crifan 185浏览 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,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
16 queries in 0.274 seconds, using 9.71MB memory