如何将CGContextDrawImage()绘制的图像旋转到中心?
在drawRect:中
CGContextSaveGState(c);
rect = CGRectOffset(rect, -rect.size.width / 2, -rect.size.height / 2);
CGContextRotateCTM(c, angle);
CGContextDrawImage(c, rect, doodad.CGImage);
CGContextRotateCTM(c, -angle);
CGContextTranslateCTM(c, pt.x, pt.y);
prevRect = rect;
CGContextRestoreGState(c);我在原点绘制图像,并在其中心旋转它,然后平移到它应该绘制的位置。不起作用。
发布于 2013-05-28 10:28:21
关于坐标变换要记住的一件事是,你不是在操纵对象,就像图形编辑器中的Transform命令一样;你是在操纵空间本身。
想象一下,这个设备被一只鹅颈臂悬挂在桌子上,并固定在适当的位置。坐标变换可以移动桌面(平移),也可以将桌面以某种方式旋转(旋转),甚至可以挤压和拉伸桌面(缩放)。
关于坐标变换需要记住的另一件事是变换总是相对于原点。想象一下,你的办公桌从一个角落开始--对应于你的视图的一个角落--就在你的视图在屏幕上结束的角落的正下方。平移移动桌面的一角,桌面的其余部分也随之移动,在屏幕下方以这样或那样的方式滑动。旋转使桌子转到那个角落。
还有一件事:转型会影响未来,而不是过去。绘制一些东西,然后试图转换它是行不通的,因为你不会转换你已经绘制的东西,而是转换你将要绘制的空间。因此,我们需要先移动桌子,然后才能将图像放在正确的位置。
(我提到最后一部分是因为您有两个转换命令,这些命令会被CGContextRestoreGState调用立即恢复。它们之间没有画图可以影响。)
因此,让我们从图像的未旋转矩形开始。
CGRect imageRect = { pointWhereYouWantTheImageCentered, doodad.size };现在,CGContextDrawImage接受一个矩形,但是,正如你所知道的,它将从矩形的左下角绘制图像,而不是从中心。(因此进行了整个练习。)
所以,这就是你要做的。最后,您将不在上面所示的点上绘制图像,而是在零点-即在桌子的最边上绘制图像。(不是居中于角落,而是图像的角落在桌子的角落。)
那该怎么做呢?这需要一些设置。你得把你的桌子移开。
首先,您需要将桌子向上和向右移动,直到桌子的原点位于所需的中心点:
CGContextTranslateCTM(context, imageRect.origin.x, imageRect.origin.y);然后进行旋转(将桌子绕其原点角旋转):
CGContextRotateCTM(context, angleInRadians);然后将桌子向下移动图像宽度和高度的一半:
CGContextTranslateCTM(context, imageRect.size.width * -0.5, imageRect.size.height * -0.5);然后,最后,把图像放在桌子的一角。
CGContextDrawImage(context, (CGRect){ CGPointZero, imageRect.size }, [doodad CGImage]);当您的办公桌移动时,该矩形的中心位于屏幕上您希望图像居中的点的正下方,因此图像是居中的。
发布于 2016-12-21 21:01:31
此函数可以在现有图像上绘制以弧度为旋转角度的图像。
Swift 3:
extension UIImage {
func putImage(image: UIImage, on rect: CGRect, angle: CGFloat = 0.0) -> UIImage{
let drawRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContextWithOptions(drawRect.size, false, 1.0)
// Start drawing self
self.draw(in: drawRect)
// Drawing new image on top
let context = UIGraphicsGetCurrentContext()!
// Get the center of new image
let center = CGPoint(x: rect.midX, y: rect.midY)
// Set center of image as context action point, so rotation works right
context.translateBy(x: center.x, y: center.y)
context.saveGState()
// Rotate the context
context.rotate(by: angle)
// Context origin is image's center. So should draw image on point on origin
image.draw(in: CGRect(origin: CGPoint(x: -rect.size.width/2, y: -rect.size.height/2), size: rect.size), blendMode: .normal, alpha:
1.0)
// Go back to context original state.
context.restoreGState()
// Get new image
let newImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}
}如果您需要创建和清空具有大小和颜色的图像,请使用:
extension UIImage {
convenience init?(size: CGSize, color: UIColor) {
let rect = CGRect(origin: .zero, size: size)
UIGraphicsBeginImageContextWithOptions(rect.size, true, 1.0)
color.setFill()
UIRectFill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
guard let cgImage = image?.cgImage else { return nil }
self.init(cgImage: cgImage)
}
}发布于 2013-05-27 12:23:07
您可以使用以下方法来旋转图像。
-(UIImage*)rotateImage:(UIImage*)img forAngle:(CGFloat)radian {
UIView *rotatedViewBox = [[UIView alloc]initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(radian);
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, rotatedSize.width, rotatedSize.height);
CGContextRotateCTM(context, radian);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGRectMake(-img.size.width, -img.size.height, img.size.width, img.size.height), img.CGImage);
UIImage *returnImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return returnImg;
}如果使用角度,则以下是用于将度数转换为弧度的宏
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)https://stackoverflow.com/questions/16766111
复制相似问题