我正在尝试使用CGAffineTransform将UIImage围绕其中心点旋转180度,但它没有按我的要求工作:/
代码:
UIImage RotateImage(UIImage imageIn) {
int kMaxResolution = 2048;
CGImage imgRef = imageIn.CGImage;
float width = imgRef.Width;
float height = imgRef.Height;
CGAffineTransform transform = CGAffineTransform.MakeIdentity ();
RectangleF bounds = new RectangleF( 0, 0, width, height );
if ( width > kMaxResolution || height > kMaxResolution )
{
float ratio = width/height;
if (ratio > 1)
{
bounds.Width = kMaxResolution;
bounds.Height = bounds.Width / ratio;
}
else
{
bounds.Height = kMaxResolution;
bounds.Width = bounds.Height * ratio;
}
}
float scaleRatio = bounds.Width / width;
SizeF imageSize = new SizeF( width, height);
float boundHeight;
//This piece of code should make this UImage upside down
transform = CGAffineTransform.MakeTranslation (imageSize.Width, imageSize.Height);
transform = CGAffineTransform.Rotate(transform, (float)Math.PI);
//=============
UIGraphics.BeginImageContext(bounds.Size);
CGContext context = UIGraphics.GetCurrentContext ();
context.ScaleCTM(scaleRatio, -scaleRatio);
context.TranslateCTM(0, -height);
context.ConcatCTM(transform);
context.DrawImage (new RectangleF (0, 0, width, height), imgRef);
UIImage imageCopy = UIGraphics.GetImageFromCurrentImageContext ();
UIGraphics.EndImageContext ();
return imageCopy;
}
此后图像会缩放,但不能上下颠倒。为什么?
谢谢您的回答。