iOS rotate UIImage using Quartz

孤街醉人 提交于 2019-12-12 00:32:04

问题


I have a UIView subclass with custom

- (void)drawRect:(CGRect)rect

I want to draw an UIImage at position 50,50 and have it rotated by 20 degrees.
I tried the following code:

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextRotateCTM (context, degreesToRad(20));
[image drawAtPoint:CGPointMake(50, 50)];
CGContextRestoreGState(context);

It rotates the image correctly, but it is not at 50,50...
How can I rotate an UIImage while maintaining the correct coordinates?


回答1:


Why are You using drawRect to draw Image?

UIImageView is optimized enough to be as fast as possible, so You could use something like this:

UIImage *image = [UIImage imageNamed:@"icon.png"];

UIImageView *imageView = [ [ UIImageView alloc ] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height) ];

imageView.image = image;

[self addSubview:imageView];

CGAffineTransform rotate = CGAffineTransformMakeRotation( 1.0 / 20.0 * M_PI );

[imageView setTransform:rotate];


来源:https://stackoverflow.com/questions/19429830/ios-rotate-uiimage-using-quartz

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!