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