2016-08-02 67 views
-1

我试图用CGContext更改我的UIImage区域的颜色,然后从当前状态创建一个新图像。原始图像具有正确的方向,但在此之后,图像变成侧向。为什么CGContextDrawImage旋转我的图像

CGRect imageRect = CGRectMake(0, 0, originalImage.size.width, originalImage.size.height); 

UIGraphicsBeginImageContextWithOptions(originalImage.size, false, originalImage.scale); 
CGContextRef context = UIGraphicsGetCurrentContext(); 

CGContextSaveGState(context); 
CGContextDrawImage(context, imageRect, originalImage.CGImage); 
CGContextSetRGBFillColor(context, 255.0f, 255.0f, 255.0f, 1.0f); 
CGContextFillRect(context, CGRectMake(5,5,100,100)); 
CGContextRotateCTM (context, radians(270)); 
CGContextRestoreGState(context); 
UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); 

回答

0

这是一个非常简单的问题。

因为iOS系统有3个不同的坐标系。

的UIKit - y轴的方向是向下 核心图形(石英) - Y轴方向为向上 的OpenGL ES - y轴的方向是向上

的originalImage是通过UIKit中创建和目标图像是由核心创建图形,所以它应该是颠倒的。

如果你想要得到正确的图像,你可以使用:

UIImageView* imageV = [[UIImageView alloc] initWithFrame:CGRectMake(100,100, 100, 100)]; 
imageV.layer.borderColor = [UIColor redColor].CGColor; 
imageV.layer.borderWidth = 1; 
imageV.image = img; 
imageV.layer.transform = CATransform3DRotate(imageV.layer.transform, M_PI, 1.0f, 0.0f, 0.0f); 
[self.view addSubview:imageV]; 

希望它可以帮助你。

相关问题