gpt4 book ai didi

ios - 如何在转换图像图形上下文后使用 drawAtPoint 绘制 UIImage

转载 作者:行者123 更新时间:2023-11-29 11:51:58 25 4
gpt4 key购买 nike

我有一个时钟指针的图像。我正在尝试创建此图像的旋转版本,以便我可以使用动画播放它(使用 UIImageView 的 animatedImages 方法)。

在下面的代码中,我看到如果我将 CGContextTranslateCTM 应用于图像图形上下文,则不会绘制图像,除非我增加我给 UIGraphicsBeginImageContextWithOptions 的大小以使其大于图片大小。

为什么需要这个?我认为所有翻译所做的就是移动 drawAtPoint: 解释 0,0 在哪里的定义?

**显示更多细节的编辑代码*

-(void) animatedClock
{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(20, 100), NO, 0);

UIBezierPath *clockHand = [[UIBezierPath alloc] init];
[[UIColor yellowColor] setFill];
[clockHand moveToPoint:CGPointMake(0, 20)];
[clockHand addLineToPoint:CGPointMake(10, 0)];
[clockHand addLineToPoint:CGPointMake(20, 20)];
[clockHand fill];

[clockHand removeAllPoints];
[clockHand moveToPoint:CGPointMake(5, 100)];
[clockHand addLineToPoint:CGPointMake(5, 20)];
[clockHand addLineToPoint:CGPointMake(15, 20)];
[clockHand addLineToPoint:CGPointMake(15, 100)];
[clockHand closePath];
[clockHand fill];


UIImage *baseImage = UIGraphicsGetImageFromCurrentImageContext();
NSLog(@"Image size is %@", NSStringFromCGSize(baseImage.size));
UIGraphicsEndImageContext();

//The screenshot will show baseimage, an arrow that is pointed upwards and at the top left of the screen
UIImageView *imageView = [[UIImageView alloc] initWithImage:baseImage];
UIGraphicsEndImageContext();
[self.view addSubview:imageView];

//This code is just to see where 100, 100 is on screen
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(100,100, 5, 5)];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];

for (NSUInteger index = 0; index <= 8; index++)
{
//UIGraphicsBeginImageContextWithOptions(baseImage.size, NO, 0); //If I keep this line then nothing is shown
UIGraphicsBeginImageContextWithOptions(CGSizeMake(500, 600), NO, 0); // If I keep this line I can see some of teh rotations
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(con, 100, 100); //I want the clock hands to be centered around 100, 100
CGContextRotateCTM(con, 45*index*M_PI/180);
[baseImage drawAtPoint:CGPointMake(0, 0)];
UIImageView *imageView = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()];
UIGraphicsEndImageContext();
[self.view addSubview:imageView];
imageView.opaque = YES;
imageView.backgroundColor = [UIColor clearColor];
[self.view addSubview:imageView];
}
}

Image showing what is coming on screen. If I use the <code>UIGraphicsBeginImageContextWithOptions(baseImage.size, NO, 0);</code> line instead of CGSizeMake(500,600) then all the rotated arrows don't show up

最佳答案

I am seeing that if I apply a CGContextTranslateCTM to the image graphics context, the image is not drawn

当然。您在 0,0 绘制。但首先您应用 100,100 的翻译 CTM。所以你实际上画了 100,100。但是上下文只有 20 点宽。因此,您完全在上下文之外进行绘制。上下文之外没有上下文,所以什么也画不出来。

关于ios - 如何在转换图像图形上下文后使用 drawAtPoint 绘制 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40733349/

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