在 xamarin iOS 中旋转 UIImage [英] Rotate UIImage in xamarin iOS

查看:26
本文介绍了在 xamarin iOS 中旋转 UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 xamarin 中旋转 UIImage.我使用此代码但未成功获取旋转图像.

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

推荐答案

试试这个,只是将Objective-C转成C#,参考这里.

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

这篇关于在 xamarin iOS 中旋转 UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆