【问题标题】:Why is CGContextDrawImage rotating my image为什么 CGContextDrawImage 旋转我的图像
【发布时间】:2016-08-02 04:27:55
【问题描述】:

我正在尝试使用CGContext 更改我的UIImage 区域的颜色,然后从当前状态创建一个新图像。原始图像具有正确的方向,但在此之后,图像会转向。

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();

【问题讨论】:

    标签: ios uiimage cgcontext


    【解决方案1】:

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

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

    UIKit - y 轴方向向下 Core Graphics(Quartz) - y轴方向向上 OpenGL ES - y轴方向向上

    原图是UIKit创建的,目标图是Core Graphics创建的,所以应该是上下颠倒的。

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

    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];
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多