Deepraj Chowrasia Deepraj Chowrasia - 3 months ago 18
Objective-C Question

HOW TO ROTATE IMAGE in circular path

I want to

rotate image
(arrow) in circular path in
coreGraphics
like this image. in circular path.

input image:

Arrow image

output after rotation:

Rotate arrow in CoreGraphics iOS

Answer

This will return rotated image

- (UIImage *)rotateImage:(UIImage *)image onDegrees:(float)degrees
{
    CGFloat rads = M_PI * degrees / 180;
    float newSide = MAX([image size].width, [image size].height);
    CGSize size =  CGSizeMake(image.size.width, newSide);
    UIGraphicsBeginImageContext(size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(ctx, image.size.width, newSide/2);
    CGContextRotateCTM(ctx, rads);
    CGRect r = CGRectMake(0,-[image size].height/2,image.size.width,image. size.height);
    [image drawInRect:r];
    UIImage *i = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return i;
}

    UIImage *arrowRotated180  = [self rotateImage:[UIImage imageNamed:@"arrowImage"] onDegrees:180];

    UIImage *arrowRotated90  = [self rotateImage:[UIImage imageNamed:@"arrowImage"] onDegrees:90];
Comments