我有一个自定义的 UIView 子类
- (void)drawRect:(CGRect)rect
我想在位置 50,50 绘制一个 UIImage 并将其旋转 20 度。
我尝试了以下代码:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextRotateCTM (context, degreesToRad(20));
[image drawAtPoint:CGPointMake(50, 50)];
CGContextRestoreGState(context);
它正确地旋转了图像,但不是在 50,50...
如何在保持正确坐标的同时旋转 UIImage?
最佳答案
为什么要使用 drawRect 来绘制图像?
UIImageView 已经优化到尽可能快,所以你可以使用这样的东西:
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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19429830/