|
@@ -134,31 +134,80 @@ namespace InABox.Mobile.iOS
|
|
|
|
|
|
private UIImage RotateImage(UIImage source, float rotation)
|
|
|
{
|
|
|
- CGImage imgRef = source.CGImage;
|
|
|
- float width = imgRef.Width;
|
|
|
- float height = imgRef.Height;
|
|
|
- CGAffineTransform transform = CGAffineTransform.MakeIdentity();
|
|
|
- RectangleF bounds = new RectangleF(0, 0, width, height);
|
|
|
+ UIImage imageToReturn = null;
|
|
|
+ float radians = -1 * ((float)(rotation * Math.PI) / 180);
|
|
|
+ bool isLandscape = false;
|
|
|
|
|
|
- float angle = Convert.ToSingle((rotation / 180f) * Math.PI);
|
|
|
- transform = CGAffineTransform.MakeRotation(angle);
|
|
|
+ var x = source.Size.Width / 2;
|
|
|
+ var y = source.Size.Height / 2;
|
|
|
|
|
|
- UIGraphics.BeginImageContext(bounds.Size);
|
|
|
+ //https://stackoverflow.com/a/8536553
|
|
|
+ CGAffineTransform transform = new CGAffineTransform((nfloat)Math.Cos(radians), (nfloat)Math.Sin(radians), -(nfloat)Math.Sin(radians), (nfloat)Math.Cos(radians), (nfloat)(x - x * Math.Cos(radians)) + (nfloat)(y * Math.Sin(radians)), (nfloat)(y - x * Math.Sin(radians) - y * Math.Cos(radians)));
|
|
|
|
|
|
- CGContext context = UIGraphics.GetCurrentContext();
|
|
|
+ var diff = (source.Size.Height - source.Size.Width) / 2;
|
|
|
+ bool translateWidthAndHeight = false;
|
|
|
+ if (rotation == 90)
|
|
|
+ {
|
|
|
+ translateWidthAndHeight = true;
|
|
|
|
|
|
- context.TranslateCTM(width / 2, height / 2);
|
|
|
- context.SaveState();
|
|
|
- context.ConcatCTM(transform);
|
|
|
- context.SaveState();
|
|
|
- context.ConcatCTM(CGAffineTransform.MakeScale(1.0f, -1.0f));
|
|
|
+ transform.Translate(diff, -diff);
|
|
|
+ }
|
|
|
+ else if (rotation == 180)
|
|
|
+ {
|
|
|
+ //Transform.Translate(image.Size.Width, -image.Size.Height);
|
|
|
+ }
|
|
|
+ else if (rotation == 270)
|
|
|
+ {
|
|
|
+ translateWidthAndHeight = true;
|
|
|
+ transform.Translate(diff, -diff);
|
|
|
+ }
|
|
|
+ else if (rotation == 360)
|
|
|
+ {
|
|
|
|
|
|
- context.DrawImage(new RectangleF(-width / 2, -height / 2, width, height), imgRef);
|
|
|
- context.RestoreState();
|
|
|
+ }
|
|
|
|
|
|
- UIImage result = UIGraphics.GetImageFromCurrentImageContext();
|
|
|
- UIGraphics.EndImageContext();
|
|
|
- return result;
|
|
|
+ if (translateWidthAndHeight)
|
|
|
+ {
|
|
|
+ //now draw image
|
|
|
+ using (var context = new CGBitmapContext(IntPtr.Zero,
|
|
|
+ (int)source.Size.Height,
|
|
|
+ (int)source.Size.Width,
|
|
|
+ source.CGImage.BitsPerComponent,
|
|
|
+ source.CGImage.BitsPerComponent * (int)source.Size.Width,
|
|
|
+ source.CGImage.ColorSpace,
|
|
|
+ source.CGImage.BitmapInfo))
|
|
|
+ {
|
|
|
+ context.ConcatCTM(transform);
|
|
|
+ context.DrawImage(new RectangleF(PointF.Empty, new SizeF((float)source.Size.Width, (float)source.Size.Height)), source.CGImage);
|
|
|
+
|
|
|
+ using (var imageRef = context.ToImage())
|
|
|
+ {
|
|
|
+ imageToReturn = new UIImage(imageRef);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //now draw image
|
|
|
+ using (var context = new CGBitmapContext(IntPtr.Zero,
|
|
|
+ (int)source.Size.Width,
|
|
|
+ (int)source.Size.Height,
|
|
|
+ source.CGImage.BitsPerComponent,
|
|
|
+ source.CGImage.BitsPerComponent * (int)source.Size.Height,
|
|
|
+ source.CGImage.ColorSpace,
|
|
|
+ source.CGImage.BitmapInfo))
|
|
|
+ {
|
|
|
+ context.ConcatCTM(transform);
|
|
|
+ context.DrawImage(new RectangleF(PointF.Empty, new SizeF((float)source.Size.Width, (float)source.Size.Height)), source.CGImage);
|
|
|
+
|
|
|
+ using (var imageRef = context.ToImage())
|
|
|
+ {
|
|
|
+ imageToReturn = new UIImage(imageRef);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return imageToReturn;
|
|
|
}
|
|
|
|
|
|
private UIImage ScaleImage(UIImage sourceImage, Size? constraints)
|