1

I have drawn a ellipse using following code with border. When i use CGContextDrawImage method i am not getting border, image is visible as ellipse shape. Otherwise i got border line of this ellipse. Actually i would like to get image with ellipse shape and also border. I got only one of these. I want to get both.

- (void)drawRect:(CGRect)rect
{
     // Drawing code
    context =UIGraphicsGetCurrentContext();
CGContextClearRect(context, self.bounds);
 CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextSetLineWidth(context, 5.0);
 CGContextAddEllipseInRect(context, self.bounds);

CGContextClip(context);
CGContextStrokePath(context);
     CGContextDrawImage(context, displayImageView.frame, displayImageView.image.CGImage);

}

Edited:

How can i solve my problem as following image as
enter image description here

The above view has image with white color boarder. I want exact like this. Please tell me any one.

Prasad G
  • 6,702
  • 7
  • 42
  • 65
  • Why are you doing `if (!displayImageView.image)`? Am I missing something here.. – maroux May 30 '13 at 12:16
  • Sorry for that one. I have edited. – Prasad G May 30 '13 at 12:18
  • A few things - `CGContextDrawPath` is supposed to clear the current path. So `CGContextIsPathEmpty(context)` should be returning `true`; can you verify this? If so, There would be no clipping. Also, do you want the border outside the circle or inside the circle? In case of outside, you should stroke path before clipping, in case of inside, you should stroke it after clipping. – maroux May 30 '13 at 12:27
  • You should try [PaintCode](http://www.paintcodeapp.com), it generates efficient and readable CoreGraphics code from something you draw. (I have no shares in this company.) – meaning-matters May 30 '13 at 12:37
  • @Mar0ux: I have edited my code. see this and help me. – Prasad G Jun 07 '13 at 08:00

1 Answers1

3

CGContextClip also resets the current path:

After determining the new clipping path, the function resets the context’s current path to an empty path.

Do this:

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, self.bounds);
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextSetLineWidth(context, 5.0);
CGContextAddEllipseInRect(context, self.bounds);

CGContextClip(context);
CGContextDrawImage(context, displayImageView.frame, displayImageView.image.CGImage);
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextSetLineWidth(context, 5.0);
CGContextAddEllipseInRect(context, self.bounds);
CGContextStrokePath(context);
maroux
  • 3,764
  • 3
  • 23
  • 32