gpt4 book ai didi

ios - CGContextRotateCTM 不适用于旋转 UIImage

转载 作者:行者123 更新时间:2023-11-28 18:30:41 25 4
gpt4 key购买 nike

我想像下面的代码一样在主屏幕上绘制一个删除按钮,就像删除应用程序按钮一样。

思路是先画十字,然后旋转45度。我的代码有什么问题?

self.deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, BADGE_SIZE, BADGE_SIZE)];

if (!deleteBtnImg) {
CGRect imgFrame = self.deleteButton.bounds;

UIGraphicsBeginImageContextWithOptions(imgFrame.size, NO, 0.0);

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);

CGFloat size = MIN(imgFrame.size.width, imgFrame.size.height);
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(imgFrame.size.width/2, imgFrame.size.height/2)
radius:size/2-RADIUS_MARGIN
startAngle:0
endAngle:M_PI * 2
clockwise:YES];
[path moveToPoint:CGPointMake(size / 2, 0)];
[path addLineToPoint:CGPointMake(size / 2, size)];
[path moveToPoint:CGPointMake(0, size / 2)];
[path addLineToPoint:CGPointMake(size, size / 2)];
[[UIColor whiteColor] setFill];
[[UIColor redColor] setStroke];
[path setLineWidth:1.0];
[path fill];
[path stroke];

CGContextRotateCTM(context, M_PI/4);

deleteBtnImg = UIGraphicsGetImageFromCurrentImageContext();
CGContextRestoreGState(context);
UIGraphicsEndImageContext();
}
[self.deleteButton setImage:deleteBtnImg forState:UIControlStateNormal];

最佳答案

您必须在开始绘制之前旋转上下文。然而,即使你在绘图前旋转。图像看起来仍然不正确。这是因为当你旋转上下文时,上下文实际上是围绕它的原点旋转的,即 (0,0) 或左下角(CoreGraphic 的坐标系与 UI 的坐标系有点不同)。所以你可以做的是,在旋转之前,平移上下文,使其围绕其中心旋转,然后在旋转后将其移回。这是一个简单的例子:

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGFloat size = MIN(imgFrame.size.width, imgFrame.size.height);
CGContextTranslateCTM(context, size / 2, size / 2);
CGContextRotateCTM(context, M_PI_4);
CGContextTranslateCTM(context, -size / 2, -size / 2);
// Start your drawing code

关于ios - CGContextRotateCTM 不适用于旋转 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31283486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com