iOS使用Quartz旋转UIImage [英] iOS rotate UIImage using Quartz
本文介绍了iOS使用Quartz旋转UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个自定义的UIView子类
I have a UIView subclass with custom
- (void)drawRect:(CGRect)rect
我想在位置50,50处绘制UIImage并将其旋转20度。
我尝试了以下代码:
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);
它可以正确旋转图像,但不是50,50 ...
如何在保持正确坐标的同时旋转UIImage?
It rotates the image correctly, but it is not at 50,50...
How can I rotate an UIImage while maintaining the correct coordinates?
推荐答案
为什么要使用drawRect绘制图像?
Why are You using drawRect to draw Image?
UIImageView已经过优化,以使其尽可能快,因此您可以使用以下方式:
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];
这篇关于iOS使用Quartz旋转UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文