在drawRect中旋转UIImage

时间:2011-03-26 12:14:27

标签: iphone uiimage rotation

我有以下代码在调用drawRect时显示图像。

  UIImage *sourceImage = [UIImage imageNamed:@"icon.png"];    


CGRect rect = CGRectMake(12.5, 12.5, 
                         sourceImage.size.width, 
                         sourceImage.size.height);

[sourceImage drawInRect:rect];

如何在绘制之前将其旋转几度,我读到的所有内容都需要它在一个看起来很重的drawView中的ImageView

2 个答案:

答案 0 :(得分:6)

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];

答案 1 :(得分:4)

旋转(或由变换矩阵完成的任何其他变换)不是“重”。所有变换基本上都是矢量和小矩阵的简单乘法(见Apple's Quartz 2D documentation for the math

在使用CGContextRotateCTM(context, radians);绘制图像之前使用[sourceImage drawInRect:rect];。如果您需要围绕另一个点旋转图像,请先使用CGContextTranslateCTM进行翻译。