OGeek|极客世界-中国程序员成长平台

标题: ios - 在 xamarin iOS 中旋转 UIImage [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:59
标题: ios - 在 xamarin iOS 中旋转 UIImage

我想在 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;
}



Best Answer-推荐答案


试试这个,把Objective-C转成C#,引用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;
}

关于ios - 在 xamarin iOS 中旋转 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49652589/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4