菜鸟教程小白 发表于 2022-12-11 19:59:08

ios - 在 xamarin iOS 中旋转 UIImage


                                            <p><p>我想在 xamarin 中旋转 UIImage。我使用此代码但未成功获取旋转图像。</p>

<pre><code>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;
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>试试这个,把Objective-C转成C#,引用<a href="https://stackoverflow.com/a/11667808/8187800" rel="noreferrer noopener nofollow">here</a> .</p>

<pre><code>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;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 xamarin iOS 中旋转 UIImage,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/49652589/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/49652589/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 xamarin iOS 中旋转 UIImage