Rotate UIImage in xamarin iOS

前端 未结 4 1686
失恋的感觉
失恋的感觉 2020-12-12 02:10

I want to rotate an UIImage in xamarin. I use this code but not successfully get the rotated image.

public UIImage RotateImage(UIImage image)
{
    CGImage i         


        
相关标签:
4条回答
  • 2020-12-12 02:58

    Maybe this can be useful:

    myImage.Transform = CGAffineTransform.MakeRotation(3.14159f * Degrees / 180f);
    
    0 讨论(0)
  • 2020-12-12 03:02

    In Xamarin.Forms with Views you have the Rotation property, no need for platform code:

    Image test = new Image { Rotation = 45};
    
    0 讨论(0)
  • 2020-12-12 03:05

    Try this one , just convert Objective-C to C#, refer to here.

    public UIImage RotateImage(UIImage image, float degree)
    {
        float Radians = degree * (float)Math.PI / 180;
    
        UIView view = new UIView(frame: new CGRect(0, 0, image.Size.Width, image.Size.Height));
        CGAffineTransform t = CGAffineTransform.MakeRotation(Radians);
        view.Transform = t;
        CGSize size = view.Frame.Size;
    
        UIGraphics.BeginImageContext(size);
        CGContext context = UIGraphics.GetCurrentContext();
    
        context.TranslateCTM(size.Width/2, size.Height/2);
        context.RotateCTM(Radians);
        context.ScaleCTM(1, -1);
    
        context.DrawImage(new CGRect(-image.Size.Width/2, -image.Size.Height/2, image.Size.Width, image.Size.Height), image.CGImage);
    
        UIImage imageCopy = UIGraphics.GetImageFromCurrentImageContext();
        UIGraphics.EndImageContext();
    
        return imageCopy;
    }
    
    0 讨论(0)
  • 2020-12-12 03:06

    Heres a method that works well:

        public async Task<byte[]> RotateImage(UIImage image, float amount)
        {
            UIImage imageToReturn = null;
            float radians = -1 * ((float)(amount * Math.PI) / 180);
            bool isLandscape = false;
    
            var x = image.Size.Width / 2;
            var y = image.Size.Height / 2;
    
            //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)));
    
            var diff = (image.Size.Height - image.Size.Width) / 2;
            bool translateWidthAndHeight = false;
            if (amount == 90)
            {
                translateWidthAndHeight = true;
    
                transform.Translate(diff, -diff);
            }
            else if (amount == 180)
            {
                //Transform.Translate(image.Size.Width, -image.Size.Height);
            }
            else if (amount == 270)
            {
                translateWidthAndHeight = true;
                transform.Translate(diff, -diff);
            }
            else if (amount == 360)
            {
    
            }
    
            if (translateWidthAndHeight)
            {
                //now draw image
                using (var context = new CGBitmapContext(IntPtr.Zero,
                                                        (int)image.Size.Height,
                                                        (int)image.Size.Width,
                                                        image.CGImage.BitsPerComponent,
                                                        image.CGImage.BitsPerComponent * (int)image.Size.Width,
                                                        image.CGImage.ColorSpace,
                                                        image.CGImage.BitmapInfo))
                {
                    context.ConcatCTM(transform);
                    context.DrawImage(new RectangleF(PointF.Empty, new SizeF((float)image.Size.Width, (float)image.Size.Height)), image.CGImage);
    
                    using (var imageRef = context.ToImage())
                    {
                        imageToReturn = new UIImage(imageRef);
                    }
                }
            }
            else
            {
                //now draw image
                using (var context = new CGBitmapContext(IntPtr.Zero,
                                                        (int)image.Size.Width,
                                                        (int)image.Size.Height,
                                                        image.CGImage.BitsPerComponent,
                                                        image.CGImage.BitsPerComponent * (int)image.Size.Height,
                                                        image.CGImage.ColorSpace,
                                                        image.CGImage.BitmapInfo))
                {
                    context.ConcatCTM(transform);
                    context.DrawImage(new RectangleF(PointF.Empty, new SizeF((float)image.Size.Width, (float)image.Size.Height)), image.CGImage);
    
                    using (var imageRef = context.ToImage())
                    {
                        imageToReturn = new UIImage(imageRef);
                    }
                }
            }
    
    
    
    
    
            using (NSData imageData = imageToReturn.AsJPEG())
            {
                Byte[] byteArray = new Byte[imageData.Length];
                System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, byteArray, 0, Convert.ToInt32(imageData.Length));
                return byteArray;
            }
        }
    
    0 讨论(0)
提交回复
热议问题