Xamarin IOS UI 线程由于 CPU 密集型方法而被阻塞,尝试了 Await/Async/Threading/Tasks 没有解决它 见下面的代码

问题描述

负责从预测中检索关键点的 GetJoints 方法会阻止/影响 UI。这可能在应用程序运行后 3.4 分钟发生,因此在启动时不会引起注意。如果我删除 GetJoints 方法,则 UI 很好并且不存在块/滞后。我只在 UI 上使用简单的动画,所以没有什么密集的,但我们可以看到它们开始被阻止。

https://github.com/tyronelinton/samplygame/blob/main/AppDelegate.cs

//方法

public static List<Joint> GetJoints(LightBuzz5Output prediction,int sourceWidth,int       sourceHeight)
    {
        var heatmaps = prediction.Final_heatmaps_0;
        var keypointCount = (int)heatmaps.Shape[2] - 1;
        var heatmapWidth = (int)heatmaps.Shape[3];
        var heatmapHeight = (int)heatmaps.Shape[4];

        List<Joint> points = new List<Joint>();

        for (int i = 0; i < keypointCount; i++)
        {
            int positionX = 0;
            int positionY = 0;

            float confidence = 0.0f;

            for (int x = 0; x < heatmapWidth; x++)
            {
                for (int y = 0; y < heatmapHeight; y++)
                {
                    int index = y + heatmapHeight * (x + heatmapWidth * i);

                    if (heatmaps[index].FloatValue > confidence)
                    {
                        confidence = heatmaps[index].FloatValue;

                        positionX = x;
                        positionY = y;
                    }
                }
            }

            points.Add(new Joint
            {
                X = (float)sourceWidth * (float)positionY / (float)heatmapHeight,Y = (float)sourceHeight * (float)positionX / (float)heatmapWidth,Confidence = confidence
            });
        }

        return points;
    }

//用法: 私有无效进程(UIImage 图像) { UIImage resizedImage = image.Scale(new CGSize(256,256));

            CVPixelBuffer buffer = Utilities.GetBuffer(resizedImage.CGImage);

            NSError error;
            var prediction = model.GetPrediction(buffer,out error);

            List<Joint> joints = null;

            if (error != null)
            {
                 System.Diagnostics.Debug.WriteLine(error);
            }
            else
            {
                joints = Utilities.GetJoints(prediction,width,height);
            }            
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)