2013-11-28 82 views
7

现在我正在开发一个项目,我需要显示具有如下形状边框的图像。更改UIImageview形状矩形的默认形状

enter image description here

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

+0

它是静态图像还是一种控件,当用户触摸图像时需要处理触摸事件?你在图像编辑器中创建图像还是需要以编程方式创建图像? – yurish

+0

@yurish:static image..The user image download from internet ..然后在上面的步骤中显示图片 – Seeker

+0

我接受了下面的答案,但它不会给出我上面提到的确切形状... – Seeker

回答

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

可能是这样的代码将帮助你...

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

:你能告诉我关于imgtest吗? – Seeker

+0

我不知道哪个对象是.. – Seeker

+0

哦对不起,这是你的原始图像 –