您的位置:首页 > 移动开发 > IOS开发

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

2016-02-29 11:52 477 查看


Tomorrow,战争解决不了问题,但能解决制造问题的人…

肖亚飞差不多得了先生、匿名用户 等人赞同

可以使用UIBezierPath的

+ (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;


如果需要将UIView的4个角全部都为圆角,做法相当简单,只需设置其Layer的cornerRadius属性即可。

发布于 2015-05-08 添加评论 感谢

分享

收藏没有帮助

举报

作者保留权利

20赞同
反对,不会显示你的姓名



Joshua
Shen,iOS

轻希扩散性百万甜面包feiyujie 等人赞同

四个角都圆角不用说,直接设置cornerRadius和maskToBounds

如果只是上面两个角圆角的话,那就画

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);


你觉得不爽,你可以自己封装嘛
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: