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

时间:2013-11-28 08:38:03

标签: ios objective-c uiimageview

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

enter image description here

我该怎么做?我不知道这样做。请任何想法解决。

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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;
}

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

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;