1

I've been struggling with this for hours. Affine transforms never seem to do what I expect them to.

CGContextSaveGState (ctx);
float w = self.frame.size.width;
float h = self.frame.size.height;
CGContextRotateCTM (ctx, angle);
CGContextTranslateCTM (ctx, w/2, -h/2);
CGRect r = CGRectMake (0,0, w, h);
CGContextDrawImage (ctx, r, image.CGImage);
CGContextRestoreGState (ctx);

I have a UIImage called image, I have a CGImage that is accessible through ctx. I want to rotate UIImage from its center by an angle, and draw it on top of the CGImage.

If I rotate and then translate, the UIImage gets streteched in a bad way. If I translate and then rotate, same result.

Any clues on how to do this? Thank you.

Fu Bar
  • 11
  • 2

1 Answers1

2

This code is currently working in iOS6.

Check if image width is greater than its height and if so rotate image clockwise; else it will load per default behavior.

- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *image = myImage;

if( image.size.width > image.size.height ) {

    UIImage *image = myImage;
    image = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationRight];

    [bgImage setImage:image];

} else {

    [bgImage setImage:image];

}

}
code-blind
  • 169
  • 1
  • 4
  • 13