Rotate UIImage in xamarin iOS

前端 未结 4 1689
失恋的感觉
失恋的感觉 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 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;
    }
    

提交回复
热议问题