Objective c CGImage在CGContext上绘制时旋转
我有下一个代码,每次用户触摸屏幕并修改UIBezierPath时都会调用它Objective c CGImage在CGContext上绘制时旋转,objective-c,ios,cgcontext,cgimage,Objective C,Ios,Cgcontext,Cgimage,我有下一个代码,每次用户触摸屏幕并修改UIBezierPath时都会调用它 CGContextRef context = UIGraphicsGetCurrentContext(); CGContextDrawImage(context, self.bounds, image); [lineColor setStroke]; [currentPath stroke]; if(panEnded) { if(image!=nil) { CFRel
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度。我做错了什么?这是坐标和方向的差异,绘制图像的最佳方法是使用UIImage上的方法,并注意处理所有这些内容
UIImage *imageUI=[UIImage imageWithCGImage:image];
[imageUI drawInRect:rect];
希望这有帮助我解决了这个问题,每次拍摄时在不同的背景下旋转图像:
- (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);
}
这是我的解释,当您尝试将CGImage绘制到上下文中时,上下文假定CGImage在像素坐标中,并旋转它,以便生成的图像在世界坐标中。 例如,如果在绘制之前尝试将图像旋转-90,然后再进行绘制,则效果良好,因为在世界坐标中,原点位于左下角,而在像素坐标中,原点位于左上角