gpt4 book ai didi

iphone - 旋转图像上下文 (CGContextRotateCTM) 使其变为空白。为什么?

转载 作者:IT王子 更新时间:2023-10-29 07:48:16 36 4
gpt4 key购买 nike

#define radians(degrees) (degrees * M_PI/180)

UIImage *rotate(UIImage *image) {
CGSize size = image.size;;

UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();

// If this is commented out, image is returned as it is.
CGContextRotateCTM (context, radians(90));

[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;
}

难道还有什么问题吗?没有想法。

最佳答案

问题是您的上下文正在围绕左上角的 (0,0) 旋转。如果您围绕左上角旋转 90 度,您的所有绘图都将超出上下文范围。您需要一个两步变换将上下文的原点移动到它的中间,然后旋转。您还需要以移动/旋转的原点为中心绘制图像,如下所示:

CGContextTranslateCTM( context, 0.5f * size.width, 0.5f * size.height ) ;
CGContextRotateCTM( context, radians( 90 ) ) ;

[ image drawInRect:(CGRect){ { -size.width * 0.5f, -size.height * 0.5f }, size } ] ;

HTH

关于iphone - 旋转图像上下文 (CGContextRotateCTM) 使其变为空白。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5983090/

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