更改UIImageview形状矩形的默认形状

See*_*ker 8 objective-c uiimageview ios

现在我正在开发一个项目,我需要显示带有边框的图像,如下面的形状.

在此输入图像描述

我怎样才能做到这一点?我不知道这样做.请任何想法解决.

Pra*_*inh 10

可能这段代码会帮你...

 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;
Run Code Online (Sandbox Code Playgroud)


yur*_*ish 1

以下是创建遮罩路径的方法:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

然后您可以将形状蒙版应用到您的图像:

const CGFloat kCurveRadius = 500.;

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = yourImageView.bounds;
maskLayer.path = [self curvedRectWithFrame:maskLayer.bounds radius:kCurveRadius].CGPath;

yourImageView.layer.mask = maskLayer;
Run Code Online (Sandbox Code Playgroud)