Iphone 旋转图像上下文(CGContextRotateCTM),使其变为空白。为什么?

Iphone 旋转图像上下文(CGContextRotateCTM),使其变为空白。为什么?,iphone,objective-c,ios,core-graphics,Iphone,Objective C,Ios,Core Graphics,还有什么不对劲吗?没有想法。问题是您的上下文正在围绕(0,0)旋转,这是左上角。如果围绕左上角旋转90度,则所有图形都将出现在上下文边界之外。需要两步变换才能将上下文的原点移动到其中间,然后旋转。您还需要围绕移动/旋转的原点绘制图像,如下所示: #define radians(degrees) (degrees * M_PI/180) UIImage *rotate(UIImage *image) { CGSize size = image.size;; UIGraphicsBegi

还有什么不对劲吗?没有想法。

问题是您的上下文正在围绕(0,0)旋转,这是左上角。如果围绕左上角旋转90度,则所有图形都将出现在上下文边界之外。需要两步变换才能将上下文的原点移动到其中间,然后旋转。您还需要围绕移动/旋转的原点绘制图像,如下所示:

#define radians(degrees) (degrees * M_PI/180)

UIImage *rotate(UIImage *image) {
  CGSize size = image.size;;

  UIGraphicsBeginImageContext(size);
  CGContextRef context = UIGraphicsGetCurrentContext();

  // If this is commented out, image is returned as it is.
  CGContextRotateCTM (context, radians(90));

  [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
  UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

  return newImage;
}

HTH

根据Nielsbot的答案,我创建了以下代码片段。请注意,最好将其设置为函数而不是代码段,以防止“复制/粘贴编程”。我还没有做到这一点,所以请随意调整

代码段:

CGContextTranslateCTM( context, 0.5f * size.width, 0.5f * size.height ) ;
CGContextRotateCTM( context, radians( 90 ) ) ;

[ image drawInRect:(CGRect){ { -size.width * 0.5f, -size.height * 0.5f }, size } ] ;
//
UIImage*imageToDrawRotated=;
浮动旋转角度=;
CGRect drawFrame=CGRectMake(,);
cgContextTranslateCm(UIGraphicsGetCurrentContext(),floor(drawFrame.origin.x+drawFrame.size.width/2),floor(drawFrame.origin.y+drawFrame.size.height/2);
CGContextRotateCTM(UIGraphicsGetCurrentContext(),rotationAngle);
[imageToDrawRotated drawInRect:CGRectMake(-floor(drawFrame.size.width/2)、-floor(drawFrame.size.height/2)、drawFrame.size.width、drawFrame.size.height)];
CGContextRotateCTM(UIGraphicsGetCurrentContext(),-rotationAngle);
CGContextTranslateCm(UIGraphicsGetCurrentContext(),-floor(drawFrame.origin.x+drawFrame.size.width/2),-floor(drawFrame.origin.y+drawFrame.size.height/2));
// 

此外,我还欢迎使用更好的方法将CGContext重置为以前的状态。

我尝试了以下代码,它可以工作,从


如果不想更改所有原点和尺寸,请移动中心,旋转并再次将中心更改为角点,如下所示:

#define radians(degrees) (degrees * M_PI/180)

UIImage *rotate(UIImage *image) {
  CGSize size = image.size;;

  UIGraphicsBeginImageContext(size);
  CGContextRef context = UIGraphicsGetCurrentContext();

  // If this is commented out, image is returned as it is.
  CGContextRotateCTM (context, radians(90));

  [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
  UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

  return newImage;
}
cgcontexttranslatecm(上下文,0.5f*size.width,0.5f*size.height);
CGContextRotateCTM(上下文,弧度(90));
CGContextTranslateCm(上下文,-0.5f*size.width,-0.5f*size.height)

然后像这样画:

#define radians(degrees) (degrees * M_PI/180)

UIImage *rotate(UIImage *image) {
  CGSize size = image.size;;

  UIGraphicsBeginImageContext(size);
  CGContextRef context = UIGraphicsGetCurrentContext();

  // If this is commented out, image is returned as it is.
  CGContextRotateCTM (context, radians(90));

  [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
  UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

  return newImage;
}

[图像绘制链接:CGRectMake(0,0,size.width,size.height)]

我喜欢@lbsweek的解决方案,但我有点担心只为转换而分配UIView,相反,我尝试了以下方法

+ (UIImage *)rotateImage:(UIImage*)src byRadian:(CGFloat)radian
{
    // calculate the size of the rotated view's containing box for our drawing space
    //UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0, src.size.width, src.size.height)];
    //CGAffineTransform t = CGAffineTransformMakeRotation(radian);
    //rotatedViewBox.transform = t;
    //CGSize rotatedSize = rotatedViewBox.frame.size;
    CGRect rect = CGRectApplyAffineTransform(CGRectMake(0,0, src.size.width, src.size.height), CGAffineTransformMakeRotation(radian));
    CGSize rotatedSize = rect.size;

    // Create the bitmap context
    UIGraphicsBeginImageContext(rotatedSize);
    CGContextRef bitmap = UIGraphicsGetCurrentContext();

    // Move the origin to the middle of the image so we will rotate and scale around the center.
    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);

    //   // Rotate the image context
    CGContextRotateCTM(bitmap, radian);

    // Now, draw the rotated/scaled image into the context
    CGContextScaleCTM(bitmap, 1.0, -1.0);
    CGContextDrawImage(bitmap, CGRectMake(-src.size.width / 2, -src.size.height / 2, src.size.width, src.size.height), [src CGImage]);

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

对,这是图像旋转的轴。但那个代码没用。总之,我找到了正确的答案。我会尽快回复+答案是1。谢谢你,伙计!陛下是旋转还是平移?我有时会混淆顺序。谢谢!这对我起了作用。后来,我以我能找到的唯一方式,通过恢复上下文来扩展这一点。让我在一个新的答案中发布它。Timo--您可以通过调用
CGContextSaveGState()
来保存转换矩阵,然后在完成后调用
CGContextRestoreGState()
。更好的方法?您只需使用CGContextSaveGState/cgcontextrestoregstate即可查看Hardy Macia的简单而精彩的代码:谢谢Hardy Macia!仅仅为了获得旋转矩形的边界而创建新视图有点疯狂。您可以将这里的前4行代码简单地替换为:
cgsizerotatedsize=CGSizeMake(src.size.height,src.size.width)@CraigMcMahon你是对的。因为我的重点是显示CTM功能,所以请使用最简单但有效的方法获取边界,当使用代码时,它应该是完美的。@CraigmCamhon正确的方法应该是这样使用:
CGSize rotatedSize=cgRectApplyAffiNetTransform(CGRectMake(0,0,src.size.width,src.size.height),CGAffineTransformMakeRotation(弧度))如果您能通过一些插图说明这是如何达到预期效果的,我将不胜感激。特别是,这是因为旋转是沿着上下文的中心进行的吗?