ios怎么让图片只有上半边圆角?_iOS




+ (UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect
                          byRoundingCorners:(UIRectCorner)corners
                                cornerRadii:(CGSize)cornerRadii

参数corners指定了想要需要成为圆角的角。可选值为:


enum {
   UIRectCornerTopLeft     = 1 << 0,
   UIRectCornerTopRight    = 1 << 1,
   UIRectCornerBottomLeft  = 1 << 2,
   UIRectCornerBottomRight = 1 << 3,
   UIRectCornerAllCorners  = ~0
};
typedef NSUInteger UIRectCorner;

从名字就能看出来其代表的意义。


例子:


UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(120, 10, 80, 80)];
view2.backgroundColor = [UIColor redColor];
[self.view addSubview:view2];
    
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view2.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = view2.bounds;
maskLayer.path = maskPath.CGPath;
view2.layer.mask = maskLayer;



CGContextRef context = UIGraphicsGetCurrentContext();

UIImage *your_image = [UIImage imageNamed: @"your_image_name"];

UIBezierPath *rectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(100, 100, 100, 100) byRoundingCorners: UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii: CGSizeMake(5, 5)];
[rectanglePath closePath];
CGContextSaveGState(context);
[rectanglePath addClip];

[your_image drawInRect: CGRectMake(100, 100, your_image.size.width, your_image.size.height)];
CGContextRestoreGState(context);



阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6