使用CALayer设置圆形头像

CALayer是屏幕上的一个具有可见内容的矩形区域,每个UIView都有一个根CALayer,其所有的绘制(视觉效果)都是在这个layer上进行的。

通过UIView的layer属性可以访问这个层。

        // 初始化UIImageView

        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

       //  添加图片

        imageView.image = [UIImage imageNamed:@"你图片的名字"];

        //设置layer的圆角,刚好是自身宽度的一半,这样就成了圆形

        imageView.layer.cornerRadius = imageView.bounds.size.width * 0.5;

        //设置边框的宽度

        imageView.layer.borderWidth = 5.0;

        //设置边框的颜色  

        imageView.layer.borderColor = [UIColor whiteColor].CGColor; 

        //告诉layer将位于它之下的layer都遮盖住

        imageView.layer.masksToBounds = YES;

        // 添加到视图上

        [self.view addSubview:imageView];

 

 

posted @ 2015-07-16 15:33  D一个就好  阅读(296)  评论(0编辑  收藏  举报