4

我无法在另一个图像上绘制我的旋转图像。我尝试了几种方法来做到这一点,但没有成功。我的 backgroundImg 没问题,但我的 logoImageView 没有旋转。为什么?这是我的代码:

CGSize newSize = CGSizeMake(555, 685);
//UIGraphicsBeginImageContext(newSize);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[backgroundImg.image drawInRect:CGRectMake(0, 0, 555, 685)];

CGAffineTransform rotate;
rotate = CGAffineTransformMakeRotation((rotationSlider.value + 360) * M_PI / 180.0);
logoImageView.layer.anchorPoint = CGPointMake (0.5, 0.5);
logoImageView.transform = CGAffineTransformMakeScale (1, -1);
[logoImageView setTransform:rotate];

然后我尝试1):

   [logoImageView.image drawAtPoint:CGPointMake(logoImageView.center.x, logoImageView.center.y)];

和 2):

[logoImageView.image drawInRect:CGRectMake(0, 0, logoImageView.bounds.size.width * 2.20, logoImageView.bounds.size.height * 2.20) blendMode:kCGBlendModeNormal alpha:1];

像这样完成绘图:

imageTwo = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

两者都不起作用 - 我的 logoImageView 没有旋转。有什么问题?我希望我的 logoImageView.image 在组合图像中旋转。

4

1 回答 1

3

你在这里做的是设置transform-property logoImageView。此属性指定应用于UIImageView自身的转换。虽然这会使图像在显示图像视图时看起来旋转,但它不会更改底层图像。
因此,当您旋转图像视图并读取图像视图的image-property 时,您仍然会获得与分配给它的图像完全相同的图像,因为变换应用于视图而不是图像本身。

您要做的是CGContext使用旋转变换将图像绘制到 。要设置此转换,您必须使用该CGContextRotateCTM功能。此函数设置“当前变换矩阵”,它指定在上下文中绘制时要应用的变换。我还使用CGContextTranslateCTM将图像移动到上下文的中心。

最终代码可能如下所示:

CGSize newSize = [flowersImage size];
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[flowersImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];

CGContextTranslateCTM(UIGraphicsGetCurrentContext(), newSize.width / 2.f, newSize.height / 2.f);
CGContextRotateCTM(UIGraphicsGetCurrentContext(), -M_PI/6.f);

[appleImage drawAtPoint:CGPointMake(0.f - [appleImage size].width / 2.f, 0.f - [appleImage size].height / 2.f)];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
于 2013-09-12T15:09:36.803 回答