iosuiviewcgcontextcgcontextdrawimage

How to get boarder of ellipse using CGContextDrawImage in ios 6.0


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.


Solution

  • 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);