在Xamarin iOS中旋转UIImage

3
我想在Xamarin中旋转UIImage。我使用了这段代码,但没有成功获取旋转后的图像。
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;
}
4个回答

4
尝试这个,只需将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;
}

谢谢,这段代码可以正确旋转图像,但是iOS会生成一个镜像和旋转后的图像。所以现在我的图像是在正确的角度,但左边部分在右侧。 - Mannu

2
也许这个可以有用:

最初的回答:

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

-1
在使用 Xamarin.Forms 中的视图时,您可以使用 Rotation 属性,无需编写平台代码:
Image test = new Image { Rotation = 45};

我需要平台特定的代码,因为我想要发送那张图片。 - Mannu
@Mannu 只需将已旋转的图像提供给平台代码即可。 - Hichame Yessou

-1
这是一个很有效的方法:
    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://dev59.com/92sy5IYBdhLWcg3w2Bk6#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;
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接