0

Hi all I need to realize borders around an UIImageView like these.

Left, top and right border like an half-moon

http://imageshack.com/a/img841/3269/o90p.png

How can i do it ?

iPatel
  • 46,010
  • 16
  • 115
  • 137
Alga85
  • 31
  • 1
  • 5
  • This one should help http://stackoverflow.com/questions/13153223/how-to-crop-the-image-using-uibezierpath – Avt May 08 '14 at 12:39

3 Answers3

0

You need to:

  1. Create a CAShapeLayer
  2. Set its path to be a CGPathRef based on view.bounds but with only two rounded corners (probably by using [UIBezierPath bezierPathWithRoundedRect:byRoundingCorners:cornerRadii:])
  3. Set your view.layer.mask to be the CAShapeLayer

Just take in account that this have a bad effect on performance. This might help

Community
  • 1
  • 1
Idan Magled
  • 2,186
  • 1
  • 23
  • 33
0

You can user CALayer for the same.

CALayer *l = [_btn layer];
[l setMasksToBounds:YES];
[l setCornerRadius:8.0];

and add QuartzCore.framework

ASHISHT
  • 305
  • 1
  • 14
0

You may want to subclass UIView and override the drawRect: method. Then place add your image view as a subview. Your drawRect: method would look something like this (with clipsToBounds set to YES):

- (void)drawRect:(CGRect)rect
{
    UIColor *fillColor = [UIColor clearColor];
    UIColor *borderColor = [UIColor redColor];
    float borderWidth = 2.0f;

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, fillColor.CGColor);
    CGContextSetLineWidth(context, borderWidth);
    CGContextSetStrokeColorWithColor(context, borderColor.CGColor);

    CGContextMoveToPoint(context, CGRectGetMinX(rect), CGRectGetMidY(rect));
    CGContextAddArc(context, CGRectGetMidX(rect), CGRectGetMidY(rect), rect.size.height/2, 2*M_PI, M_PI, 1);
    CGContextClosePath(context);
    CGContextDrawPath(context, kCGPathFillStroke);
}

Alternatively you can look into masking with a semicircle image or creating a CAShapeLayer for the image view.

sooper
  • 5,991
  • 6
  • 40
  • 65