Search code examples
objective-cioscgcontextcgimage

CGImage rotating when i draw it on CGContext


I have next code which is called every time user touches the screen and modifies the 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;
  }

so every time i create new image (from "if" branch) it rotates 180 degrees. what am i doing wrong?


Solution

  • I solved the problem by rotating image in separate context each time it is taken:

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