0

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?

kavalerov
  • 390
  • 3
  • 13

3 Answers3

0

Its the differences in coordinates and orientation, best way to draw image is using method on UIImage with takes care of dealing with all that stuff

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

Hope this helped

stringCode
  • 2,274
  • 1
  • 23
  • 32
0

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

}
kavalerov
  • 390
  • 3
  • 13
0

this is my explanation , when you try to draw a CGImage into a context , the context assumes that the CGImage is in pixel coordinates and rotates it so that the resultant image is in world coordinates. For e.g. if you try to rotate the image by -90 before you draw , and then draw , it will work fine because in world coordinates the origin is at the Lower Left Corner whereas in pixel coordinates the origin is at the Upper left corner.