Search code examples
iosrotationcore-graphicscgcontextref

CGContextRef rotation not working


I wanted to rotate the contents of the CGContextRef with -90 degrees. I found some code but it is not working correctly.

CGContextRef tempColorRef = CGBitmapContextCreate(NULL, width, height, bitsPerComponent,
                                                  bytesPerRow, colorspace,
                                                  kCGImageAlphaPremultipliedLast);
CGImageRef colorImageRef = CGBitmapContextCreateImage(colorContextRef);
CGContextTranslateCTM(tempLayerRef, width / 2, height / 2);
CGContextRotateCTM(tempLayerRef, DegreesToRadians(-90));
CGContextScaleCTM(tempColorRef, 1.0, -1.0);
CGContextDrawImage(tempColorRef, CGRectMake(-width / 2, -height / 2, width, height), colorImageRef);
CGImageRelease(colorImageRef);

colorContextRef = tempColorRef;

Anyone help me please.


Solution

  • According to the H2CO3's comments, I tried various transformations.

    It was my fault. width and height were size_t variables, so these can't hold minus values. The error was happening in the following instruction.

    CGRectMake(-width / 2, -height / 2, width, height)

    It's working fine now.