iOS 17 中弃用了一堆 UIGraphics 方法,并且没有明确的替代方案?

问题描述 投票:0回答:1

我有一段代码,其中使用了 UIGraphics 方法,似乎我使用的三种方法都已弃用。

这是一段 Xamarin.iOS 代码,但我确信 Swift 用户也可以在这里提供帮助,任何人都可以帮助我了解这些的新方法是什么,我正在尝试用 google 搜索,但我似乎没有得到关于这些方法的替代品的任何直接线索。

以下三种方法已被弃用;

    UIGraphics.BeginImageContextWithOptions(size, false, ScreenDensity);
    var image = UIGraphics.GetImageFromCurrentImageContext();
    UIGraphics.EndImageContext();

完整的代码如下,不确定这是否有帮助......

private UIImage CreateBufferImage()
{
    if (paths is null || paths.Count == 0)
    {
        return null;
    }

    var size = Bounds.Size;
    UIGraphics.BeginImageContextWithOptions(size, false, ScreenDensity);
    var context = UIGraphics.GetCurrentContext();

    context.SetLineCap(CGLineCap.Round);
    context.SetLineJoin(CGLineJoin.Round);

    foreach (var path in paths)
    {
        context.SetStrokeColor(path.Color.CGColor);
        context.SetLineWidth(path.Width);

        context.AddPath(path.Path.CGPath);
        context.StrokePath();

        path.IsDirty = false;
    }

    var image = UIGraphics.GetImageFromCurrentImageContext();

    UIGraphics.EndImageContext();

    return image;
}

谢谢

ios swift xamarin xamarin.ios maui
1个回答
0
投票

您现在应该使用

UIGraphicsImageRenderer
API 来绘制图像。

var renderer = new UIGraphicsImageRenderer(size, new UIGraphicsImageRendererFormat {
    Opaque = false,
    Scale = ScreenDensity
});

您应该将

Action<UIGraphicsImageRendererContext>
传递给
CreateImage
来绘制图像。图像将由
CreateImage
返回。

var image = renderer.CreateImage(imageContext => {
    // if you want a CGContext, you can get that from the CGContext property
    var cgcontext = imageContext.CGContext;

    // drawing code here...
});
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.