CGContextTranslateCTM(context, 0, size.height);
CGContextScaleCTM(context, 1, -1);
Just flips the image from upside down to right side up. To rotate the
image by 90 degrees, you need to something more like:
CGContextTranslateCTM(context, 0, size.height);
CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, size.width/2., size.height/2.);
CGContextRotateCTM(context, M_PI/2.);
CGContextTranslateCTM(context, -size.height/2., -size.width/2.);
That is, move the 0,0 of the coordinate system to the center of the
image, rotate is by PI/2 radians (90 degrees), then move the
coordinate system back to zero zero.
(Not tested.)