Ios 更改UIImageview形状矩形的默认形状
现在,我正在开发一个项目,我需要显示的图像与边框如下形状Ios 更改UIImageview形状矩形的默认形状,ios,objective-c,uiimageview,Ios,Objective C,Uiimageview,现在,我正在开发一个项目,我需要显示的图像与边框如下形状 我该怎么做?我不知道该怎么做。请提供任何解决方案。以下是创建掩蔽路径的方法: - (UIBezierPath *)curvedRectWithFrame:(CGRect)frame radius:(CGFloat)radius { double halfFrameHeight = ((double)frame.size.height / 2); // Check if the radius is too small.
我该怎么做?我不知道该怎么做。请提供任何解决方案。以下是创建掩蔽路径的方法:
- (UIBezierPath *)curvedRectWithFrame:(CGRect)frame radius:(CGFloat)radius
{
double halfFrameHeight = ((double)frame.size.height / 2);
// Check if the radius is too small.
if (radius < halfFrameHeight) {
radius = halfFrameHeight;
}
CGFloat arcAngle = asin(halfFrameHeight/radius);
CGFloat centerX = frame.origin.x + (frame.size.width - radius);
CGFloat centerY = frame.origin.y + halfFrameHeight;
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:frame.origin];
[path addLineToPoint:CGPointMake(centerX + radius * cos(arcAngle), frame.origin.y)];
[path addArcWithCenter:CGPointMake(centerX, centerY) radius:radius startAngle:-arcAngle endAngle:arcAngle clockwise:YES];
[path addLineToPoint:CGPointMake(frame.origin.x, path.currentPoint.y)];
[path closePath];
return path;
}
也许这段代码会帮助你
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:YourImageVIew.bounds byRoundingCorners:(UIRectCornerTopRight | UIRectCornerBottomRight) cornerRadii:CGSizeMake(50.0, 50.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = YourImageVIew.bounds;
maskLayer.path = maskPath.CGPath;
YourImageVIew.layer.mask = maskLayer;
它是一个静态图像还是一种控件,当用户触摸图像时,您需要处理触摸事件?您是在图像编辑器中创建图像还是需要以编程方式创建图像?@yurish:static image..用户图像从internet下载。。然后在上面的步骤中显示图像我接受了下面的答案,但它不会给出我上面提到的确切形状…@user2922837 ok no prob bro…要构建所需的形状,请使用
[UIBezierPath-bezierPath]
创建maskPath,然后使用addLineToPoint:
添加每个形状段<代码>添加ArcWithCenter:closePath:
UIBezierPath类的方法:你能告诉我imgtest吗?哦,对不起,这是你的原创image@user2922837现在检查我的更新答案。。。我在我的应用程序中检查了这个代码,它对我有用。从你的方法来看,我没有得到确切的形状want@user2922837根据您的要求更改拐角半径值
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:YourImageVIew.bounds byRoundingCorners:(UIRectCornerTopRight | UIRectCornerBottomRight) cornerRadii:CGSizeMake(50.0, 50.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = YourImageVIew.bounds;
maskLayer.path = maskPath.CGPath;
YourImageVIew.layer.mask = maskLayer;