1

I need to generate a circular image from a rectangular image for an UIImageView. Currently I'm drawing it with Bezier path like this:

+ (UIImage *)jsq_circularImage:(UIImage *)image withDiameter:(NSUInteger)diameter highlightedColor:(UIColor *)highlightedColor
{
    NSParameterAssert(image != nil);
    NSParameterAssert(diameter > 0);

    CGRect frame = CGRectMake(0.0f, 0.0f, diameter, diameter);
    UIImage *newImage = nil;

    UIGraphicsBeginImageContextWithOptions(frame.size, NO, [UIScreen mainScreen].scale);
    {
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSaveGState(context);

        UIBezierPath *imgPath = [UIBezierPath bezierPathWithOvalInRect:frame];
        [imgPath addClip];
        [image drawInRect:frame];

        if (highlightedColor != nil) {
            CGContextSetFillColorWithColor(context, highlightedColor.CGColor);
            CGContextFillEllipseInRect(context, frame);
        }

        newImage = UIGraphicsGetImageFromCurrentImageContext();

        CGContextRestoreGState(context);
    }
    UIGraphicsEndImageContext();

    return newImage;
}

The problem with this method is that its performance is very suboptimal (takes about 0.3 seconds for a small image).

I can't use cornerRadius with clipToBounds as I apply a spin animation to the view.

Other than preprocessing original images to make them circular, are there any other options?

Nikolay Derkach
  • 1,734
  • 2
  • 22
  • 34

0 Answers0