iOS 使用 Quartz 旋转 UIImage

标签 ios rotation uiimage coordinates quartz-2d

我有一个自定义的 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/

相关文章:

ios - 隐藏自定义工具栏按钮

ios - 如何遍历结构的静态常量?

swift - 裁剪(至 : CGRect) doesn't crop correctly

ios - 如何删除UIImage背景颜色并使之透明?

javascript - 使用 three.js 和 tween.js 以 90 度为增量旋转对象以创建 360 度循环

ios - UIImage 从 iOS 图像库中选择

ios - iOS 中的 Crashlytics 无法继续 "Build and run your app"步骤

ios - 如何使用 PromiseKit 处理仅 header 响应?

c++ - 以纵横比旋转二维 vector

javascript - 旋转具有两个数据点的 Chart.js 饼图,以在水平轴上对齐两个扇区