简体   繁体   English

在 xamarin iOS 中旋转 UIImage

[英]Rotate UIImage in xamarin iOS

I want to rotate an UIImage in xamarin.我想在 xamarin 中旋转 UIImage。 I use this code but not successfully get the rotated image.我使用此代码但未成功获得旋转图像。

public UIImage RotateImage(UIImage image)
{
    CGImage imgRef = image.CGImage;
    float width = imgRef.Width;
    float height = imgRef.Height;
    CGAffineTransform transform = CGAffineTransform.MakeIdentity();
    RectangleF bounds = new RectangleF(0, 0, width, height);
    transform = CGAffineTransform.MakeRotation((float)Math.PI / 4);
    UIGraphics.BeginImageContext(bounds.Size);
    CGContext context = UIGraphics.GetCurrentContext();

    context.ConcatCTM(transform);
    context.DrawImage(new RectangleF(0, 0, width, height), imgRef);

    UIImage imageCopy = UIGraphics.GetImageFromCurrentImageContext();
    UIGraphics.EndImageContext();

    return imageCopy;
}

Try this one , just convert Objective-C to C#, refer to here .试试这个,只是将 Objective-C 转换为 C#,参考这里

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

也许这可能有用:

myImage.Transform = CGAffineTransform.MakeRotation(3.14159f * Degrees / 180f);

在 Xamarin.Forms with Views 你有Rotation属性,不需要平台代码:

Image test = new Image { Rotation = 45};

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM