【问题标题】:CGImage rotating when i draw it on CGContext当我在 CGContext 上绘制时 CGImage 旋转
【发布时间】:2012-09-20 03:56:18
【问题描述】:

我有下一个代码,每次用户触摸屏幕并修改 UIBezierPath 时都会调用它

  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextDrawImage(context, self.bounds, image);

  [lineColor setStroke];
  [currentPath stroke];
  if(panEnded)
  {
    if(image!=nil)
    {
      CFRelease(image);
    }
    image=nil;
    image=CGBitmapContextCreateImage(context);
    panEnded=false;
  }

所以每次我创建新图像(来自“if”分支)时,它都会旋转 180 度。我做错了什么?

【问题讨论】:

    标签: objective-c ios cgcontext cgimage


    【解决方案1】:

    它的坐标和方向的差异,绘制图像的最佳方法是使用 UIImage 上的方法来处理所有这些东西

    UIImage *imageUI=[UIImage imageWithCGImage:image];   
    [imageUI drawInRect:rect];
    

    希望这有帮助

    【讨论】:

      【解决方案2】:

      我通过在每次拍摄时在单独的上下文中旋转图像来解决问题:

      - (void)drawRect:(CGRect)rect
      {
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image),CGImageGetHeight(image)), image);
        [lineColor setStroke];
        [currentPath stroke];
        if(panEnded)
        {
          if(image!=nil)
          {
            CFRelease(image);
          }
          image=nil;
          image=CGBitmapContextCreateImage(context);
          [self rotateCGImage];
          panEnded=false;
          currentPath=nil;
        }
      }
      
      -(void)rotateCGImage
      {
        UIGraphicsBeginImageContext(CGSizeMake(CGImageGetWidth(image), CGImageGetHeight(image)));
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextDrawImage(context, CGRectMake(0 , 0, CGImageGetWidth(image), CGImageGetHeight(image)), image);
      
      
        CGContextRotateCTM (context, radians(90));
        if(image!=nil)
        {
          CFRelease(image);
        }
        image=nil;
        image=CGBitmapContextCreateImage(context);
      
      }
      

      【讨论】:

        【解决方案3】:

        这是我的解释,当您尝试将 CGImage 绘制到上下文中时,上下文假定 CGImage 位于像素坐标中并对其进行旋转,以便生成的图像位于世界坐标中。 例如如果您在绘制之前尝试将图像旋转 -90,然后绘制,它将正常工作,因为在世界坐标中,原点位于左下角,而在像素坐标中,原点位于左上角。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-05-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多