ios - 在 xamarin iOS 中旋转 UIImage

标签 ios xamarin xamarin.forms xamarin.ios

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

最佳答案

试试这个,只需将 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/

相关文章:

ios - 如何在 Swift 中显示框架(SDK)中的 View Controller ?

ios - NSDate 语言环境在将 NSDateFormatter 与 NSString 和 NSDate 一起使用时导致其他语言环境出现问题

android - Xamarin android布局-行项目的透明背景

c# - sqlite net extensions InsertOrReplaceWithChildren 不插入子外键字段

android - 显示提示以了解位置

xamarin - 使用 droid 项目将 ios 项目添加到 xamarin forms 解决方案

listview - 在 ListView Xamarin 表单中滚动时图像消失

ios - 将 UIButton 添加到 SKScene

iphone - XMLParser iPhone SDK

xamarin - 无法解析来自 Xamarin.iOS 的 "System.Void ObjCRuntime.UserDelegateTypeAttribute::.ctor(System.Type)"引用