一堆 UIGraphics 方法在 iOS 17 中被弃用,没有明确的替代品?

A bunch of UIGraphics methods were deprecated in iOS 17 with no clear replacement?

提问人:FreakyAli 提问时间:11/16/2023 更新时间:11/16/2023 访问量:101

问:

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

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

以下三种方法已被弃用;

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

谢谢

swift xamarin xamarin.ios 毛伊岛

评论

1赞 MadProgrammer 11/16/2023
developer.apple.com/forums/thread/731227
1赞 HangarRash 11/16/2023
在 Swift 中,您将使用旧的相关函数来代替。UIGraphicsImageRendererUIGraphicsBeginImageContextWithOptions

答:

5赞 Sweeper 11/16/2023 #1

现在,您应该使用 UIGraphicsImageRenderer API 来绘制图像。

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

应传入 to CreateImage 来绘制图像。图像将由 返回。Action<UIGraphicsImageRendererContext>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...
});