UITableViewCell性能优化之圆角头像的实现

前阵子的项目中,遇到一个需要用一个TableView展示用户状态的需求,并且用户头像为圆形。我的第一反应是利用UIImageView的layer的圆角属性来设置,即以下两行代码

layer.cornerRadius = image?.size.width * 0.5

layer.masksToBounds = true

这样写固然可以,但是只要涉及到“layer.masksToBounds”的操作必然因为其渲染效果的问题影响TableView的性能以及整个APP的流畅度,因此如果在数据量比较大的情况下要针对以上代码进行优化,这里就得用到Quartz2D的绘图了,下面直接上代码:

这里是基于SDWebImage对获取的网络数据进行处理

func getRoundProfileImage(urlString:String){

SDWebImageDownloader.sharedDownloader().downloadImageWithURL(NSURL(string: urlString), options:SDWebImageDownloaderOptions(rawValue:0), progress: { (,) ->Voidin}) {

(image,,,_) -> Voidinifimage !=nil {

image.roundProfile({

(roundImage) -> ()inself.image = roundImage

})

}else{

UIImage(named:"avatar_default_big")?.roundProfile({

(roundImage) -> ()inself.image = roundImage})

}

}

}

这里是对image的处理,也就是上文中的“roundProfile(success:(roundImage:UIImage)->())”方法,具体实现是这样的:

func roundProfile(success:(roundImage:UIImage)->()) {

dispatch_async(dispatch_get_global_queue(0,0)) { () -> Voidinlet size:CGSize=self.size

//开启图片上下文

UIGraphicsBeginImageContextWithOptions(size,false,0)

let ctx = UIGraphicsGetCurrentContext()

//绘制路径let path =UIBezierPath()

//描述路径

path.addArcWithCenter(CGPoint(x: size.width0.5,y: size.height0.5), radius: size.height*0.5, startAngle:0.0, endAngle:CGFloat(M_PI) *2,

clockwise:false)CGContextAddPath(ctx,

path.CGPath)CGContextClip(ctx)self.drawAtPoint(CGPointZero)

let clipImage =

UIGraphicsGetImageFromCurrentImageContext()UIGraphicsEndImageContext()

dispatch_async(dispatch_get_main_queue(), { () -> Voidinsuccess(roundImage: clipImage) })

}

}

注:这里处理图片是一个耗时操作,因此需要在子线程执行;由于是异步操作,因此需要利用闭包作为方法的回调的参数来获取处理完成以后的图片。

转载于:https://juejin.im/post/5b9633e66fb9a05d353c5a2f

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值