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

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

可能有其他错误吗?从想法。

问题是你的上下文被旋转(0,0),这是左上角。如果围绕左上角旋转90度,则所有绘制都将出现在上下文的边界之外。你需要一个两步变换移动上下文的原点到它的中间,然后旋转。此外,你需要绘制你的图像为中心移动/旋转的原点,如下所示:

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

HTH

http://stackoverflow.com/questions/5983090/rotating-an-image-context-cgcontextrotatectm-causing-it-to-become-blank-why

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:iphone – 旋转图像上下文(CGContextRotateCTM),使其变为空白。为什么?