美文网首页
设置圆角

设置圆角

作者: AlanGe | 来源:发表于2017-08-16 22:51 被阅读8次
/*********** *********** 第一种 *********** ***********/
self.imgView.layer.cornerRadius = 50;
self.imgView.layer.masksToBounds = YES;
/*********** *********** 第二种 *********** ***********/
//  GEView.h
//  图片裁剪
#import <UIKit/UIKit.h>

@interface GEView : UIView
@end

//  GEView.m
//  图片裁剪
#import "GEView.h"

@implementation GEView

- (void)drawRect:(CGRect)rect {
    //1.裁剪--必须要先裁剪
    //裁剪是裁剪的上下文
    //剪切出来显示区域
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextAddEllipseInRect(ctx, rect);
    CGContextClip(ctx);
    
    //2.有图片
    //绘制的图片是在原来的上下文为坐标系的
    UIImage *image = [UIImage imageNamed:@"me2"];
    [image drawAtPoint:CGPointZero];
}
@end
/*********** *********** 第三种 *********** ***********/
- (UIImage *)imageWithName:(NSString *)imageName {
    UIImage *image = [UIImage imageNamed:imageName];
    //1.开启上下文
    UIGraphicsBeginImageContext(image.size);
    //2.获得上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    // 3.裁剪
    //3.1 准备裁剪路径
    CGContextAddArc(ctx, image.size.width/2, image.size.height/2, image.size.width/2, 0, M_PI * 2, 1);
    // 3.2 裁剪上下文
    CGContextClip(ctx);
    
    //4.绘制图片
    [image drawAtPoint:CGPointZero];
    
    //5.从上下文中获得最新的图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //6.关闭上下文
    UIGraphicsEndImageContext();
    
    return newImage;
}

// 使用
self.imageView.image = [self imageWithName:@"me2”];
/*********** *********** 第四种 *********** ***********/
// 第四种方法:使用贝塞尔曲线UIBezierPath和Core Graphics框架画出一个圆角
- (void)test4 {
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 300, 100, 100)];
    imageView.image = [UIImage imageNamed:@"me2"];
    
    //开始对imageView进行画图
    UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);
    //使用贝塞尔曲线画出一个圆形图
    [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip];
    [imageView drawRect:imageView.bounds];
    
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    //结束画图
    UIGraphicsEndImageContext();
    [self.view addSubview:imageView];
}
/*********** *********** 第五种 *********** ***********/
// 第五种方法:使用CAShapeLayer和UIBezierPath设置圆角
-(void)test5 {
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 500, 100, 100)];
    imageView.image = [UIImage imageNamed:@"me2"];
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:imageView.bounds.size];
    
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
    //设置大小
    maskLayer.frame = imageView.bounds;
    //设置图形样子
    maskLayer.path = maskPath.CGPath;
    imageView.layer.mask = maskLayer;
    [self.view addSubview:imageView];
}
/*********** *********** 第六种 *********** ***********/
/*
第六种方法:通过混合图层
此方法就是在要添加圆角的视图上再叠加一个部分透明的视图,只对圆角部分进行遮挡。其实就是中间圆形部分透明,不遮挡底部的控件,不过同时也需要遮挡颜色和view背景色一致才行。
此方法虽然是最优解,没有离屏渲染,没有额外的CPU计算,但是应用范围有限。
*/

相关文章

  • Image

    直接圆角图片 设置圆角图片度数 设置圆角图片带灰色圆角边框 设置圆角图片带灰色圆角边框带阴影

  • iOS 设置UI控件圆角

    一: 设置UIView上方圆角或者下方圆角 //设置UITableView section 圆角 设置好以后就这样了

  • iOS Masonry布局(四) - 视图设置圆角

    视图设置任意圆角 Masonry布局视图设置圆角 若使用Masonry布局的视图设置后发现,设置的圆角不起作用。这...

  • iOS Masonry - 视图设置圆角

    视图设置任意圆角 Masonry布局视图设置圆角 若使用Masonry布局的视图设置后发现,设置的圆角不起作用。这...

  • 按钮圆角

    //设置为圆角 [<#name#>.layer setMasksToBounds:YES]; //设置圆角弧度 [...

  • 玩转CALayer视觉效果

    圆角: cornerRadius 设置圆角的半径 边框: borderWidth 和borderColor 设置边...

  • 2019-01-31

    设置柱子的圆角。itemStyle 设置上右下左的圆角弧度

  • iOS设置圆角的四种方法

    原文iOS设置圆角的四种方法iOS设置圆角的方法及指定圆角的位置 一、设置CALayer的cornerRadius...

  • 边框效果-css-v1.0.0

    半透边框 通过设置颜色的透明度实现。 框内圆角 方式1:为元素设置圆角,外层设置轮廓outline。圆角与直角之间...

  • iOS图片圆角设置汇总

    点击下载 Demo 一、CornerRadius 设置圆角 如果仅仅是view需要设置圆角,是不要设置maskTo...

网友评论

      本文标题:设置圆角

      本文链接:https://www.haomeiwen.com/subject/adbwrxtx.html