在drawRect中旋转UIImage

Lee*_*ong 0 iphone rotation uiimage

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

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


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

[sourceImage drawInRect:rect];
Run Code Online (Sandbox Code Playgroud)

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

Kir*_*odd 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];
Run Code Online (Sandbox Code Playgroud)

  • 只有注释:可能更好地使用M_PI而不是3.14,为清晰起见,如果你没有别的,不要忘记如果你设置了一个变换就不应该使用'frame',这似乎经常会阻止接口上的正确行为方向变化. (2认同)