2013-11-28 81 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

哦對不起,這是你的原始圖像 –