在drawRect中旋转UIImage

发布于 2024-10-26 23:51:38 字数 369 浏览 1 评论 0原文

我有以下代码,可以在调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

脸赞 2024-11-02 23:51:38
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 / 180.0 * 3.14 );
[imageView setTransform:rotate];
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 / 180.0 * 3.14 );
[imageView setTransform:rotate];
甜心 2024-11-02 23:51:38

旋转(或由变换矩阵完成的任何其他变换)并不“重”。所有变换基本上都是向量和小矩阵的简单乘法(请参阅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 with CGContextTranslateCTM first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文