在drawRect中旋转UIImage
我有以下代码,可以在调用 drawRect 时显示图像。
UIImage *sourceImage = [UIImage imageNamed:@"icon.png"];
CGRect rect = CGRectMake(12.5, 12.5,
sourceImage.size.width,
sourceImage.size.height);
[sourceImage drawInRect:rect];
我怎样才能在绘制之前将其旋转一定度数,我读到的所有内容都需要它在 ImageView 中,这在 drawRect 中看起来很重
I have the following code that displays an image when the drawRect is called.
UIImage *sourceImage = [UIImage imageNamed:@"icon.png"];
CGRect rect = CGRectMake(12.5, 12.5,
sourceImage.size.width,
sourceImage.size.height);
[sourceImage drawInRect:rect];
How can I get this rotated by a number of degrees before it gets drawn, everything I read needs it in a ImageView which seems heavy in a drawRect
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
旋转(或由变换矩阵完成的任何其他变换)并不“重”。所有变换基本上都是向量和小矩阵的简单乘法(请参阅Apple 的 Quartz 2D 数学文档)
在绘制之前使用
CGContextRotateCTM(context, radians);
带有[sourceImage drawInRect:rect];
的图像。如果您需要围绕另一个点旋转图像,请先使用 CGContextTranslateCTM 进行平移。Rotating (or any other transformation done by a transformation matrix) is not "heavy". All the transforms basically are simple multiplications of a vector and a small matrix (see Apple's Quartz 2D documentation for the math)
Use
CGContextRotateCTM(context, radians);
before you draw the image with[sourceImage drawInRect:rect];
. If you need to rotate the image around another point, translate withCGContextTranslateCTM
first.