1

i have a rectangular UIImage and i want make it rounded and with a border, i have found a question on SO to make it rounded and works, this is the code:

- (UIImage*) roundCorneredImage: (UIImage*) orig radius:(CGFloat) r {
    UIGraphicsBeginImageContextWithOptions(orig.size, NO, 0);
    [[UIBezierPath bezierPathWithRoundedRect:(CGRect){CGPointZero, orig.size}
                                cornerRadius:r] addClip];
    [orig drawInRect:(CGRect){CGPointZero, orig.size}];
    UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return result;
}

but know i'm trying with no result to make a white border around the image how i can do?

Piero
  • 9,173
  • 18
  • 90
  • 160

2 Answers2

4

Try this :

- (UIImage*) roundCorneredImage: (UIImage*) orig radius:(CGFloat) r {
    UIGraphicsBeginImageContextWithOptions(orig.size, NO, 0);
    UIBezierPath *bezierPath =[UIBezierPath bezierPathWithRoundedRect:(CGRect){CGPointZero, orig.size}
                                                         cornerRadius:r];
    [bezierPath setLineWidth:6.0];
    [[UIColor whiteColor] setStroke];
    [bezierPath stroke];
    [bezierPath addClip];

    [orig drawInRect:(CGRect){CGPointZero, orig.size}];
    UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return result;
}
Kujey
  • 1,122
  • 6
  • 17
0

You can work with the UIImageView in which it's displayed in the following way:

UIImageView *anImageView = [[UIImageView alloc] initWithImage:[UIImage imageName:@"your_image"]]; 
anImageView.layer.masksToBounds = YES;
anImageView.layer.borderWidth = 0.5f;
anImageView.layer.borderColor = [UIColor darkGrayColor].CGColor;
anImageView.layer.cornerRadius = 8.0f;
Barbara R
  • 1,057
  • 5
  • 18