Hi all,
I am playing with affine transformations. Right now, I am trying to rotate an image, by say, 90 degrees. I have made a UIView subclass and overridden its drawRect: method like so:
When I run this code, the simulator screen remains blank. If I comment out the call to CGContextRotateCTM and uncomment any of the other transformations, they work, and the image is displayed properly, except the transformation made by CGAffineTransformMakeRotation. If I use the commented code below, which relies on a UIImageView object to do the transform, it works.
I don't understand why it wouldn't work with the CGContextRotateCTM or CGAffineTransformMakeRotation functions. Can anyone shed any light on this? It will be greatly appreciated. Thanks!
I am playing with affine transformations. Right now, I am trying to rotate an image, by say, 90 degrees. I have made a UIView subclass and overridden its drawRect: method like so:
Code:
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGContextRef context;
context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGAffineTransform t0 =CGContextGetCTM(context);
t0 =CGAffineTransformInvert(t0);
CGContextConcatCTM(context, t0);
CGAffineTransform t1 = CGAffineTransformIdentity;
//t1= CGAffineTransformMakeRotation(90);
//t1 = CGAffineTransformMakeTranslation(100, 100);
//t1=CGAffineTransformMakeScale(2, 3);
//CGContextConcatCTM(context, t1);
CGContextRotateCTM(context, 3.141592);
NSString *img_path = [[NSBundle mainBundle] pathForResource:@"image_preview" ofType:@"jpg"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:img_path];
CGImageRef img_ref = [image CGImage];
CGRect rectangle = CGRectMake(120, 120, 200, 200);
CGContextDrawImage(context, rectangle, img_ref);
//This approach works
//UIImageView *img_view = [[UIImageView alloc] initWithImage:image];
//[self addSubview:img_view];
//[img_view setTransform:t1];
CGContextRestoreGState(context);
}
When I run this code, the simulator screen remains blank. If I comment out the call to CGContextRotateCTM and uncomment any of the other transformations, they work, and the image is displayed properly, except the transformation made by CGAffineTransformMakeRotation. If I use the commented code below, which relies on a UIImageView object to do the transform, it works.
I don't understand why it wouldn't work with the CGContextRotateCTM or CGAffineTransformMakeRotation functions. Can anyone shed any light on this? It will be greatly appreciated. Thanks!