【问题标题】:ScrollView cause CPU Usage to 100% in SwiftUIScrollView 在 SwiftUI 中导致 CPU 使用率达到 100%
【发布时间】:2020-10-06 11:12:09
【问题描述】:

我正在尝试创建图像的水平列表,并在 SwiftUI 中编写了以下代码,但我不确定这里出了什么问题,但 CPU 使用率和 Ram 达到了最高水平。

我认为这是因为 WebImage 但它也不适用于 Rectangle 所以一定有一些与我编写的代码相关的东西。

private let reader = UIScreen.main.bounds

var body: some View {
    ScrollView(Axis.Set.horizontal, showsIndicators: false) {
            HStack(spacing: 0 ) {
                ForEach(self.images, id: \.self){ imageURL in
                    Rectangle()
                        .frame(width:self.reader.size.width, height: self.reader.size.height, alignment: .center)
                        .background(Color.green)
                        .border(Color.black,width: 10.0)
//                            WebImage(url: URL.init(string: imageURL))
//                                .resizable()
//                                .indicator(.activity) // Activity Indicator
//                                .scaledToFill()
//                                .frame(width:self.reader.size.width, height: self.reader.size.height, alignment: .center)
//                                .background(Color.red)
                }
            }
    }.onAppear {
        UIScrollView.appearance().isPagingEnabled = true
    } 
}

任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: swift swiftui scrollview xcode11


    【解决方案1】:

    假设您试图让每个矩形/图像填充屏幕并水平滚动,那么这应该可以完成工作:

    var body: some View {
        GeometryReader { proxy in
            ScrollView(Axis.Set.horizontal, showsIndicators: false) {
                HStack(spacing: 0 ) {
                    ForEach(self.images, id: \.self){ imageURL in
                        Color.green
                            .frame(width: proxy.size.width, height: proxy.size.height, alignment: .center)
                            .border(Color.black,width: 10.0)
    //                              WebImage(url: URL.init(string: imageURL))
    //                                  .resizable()
    //                                  .indicator(.activity) // Activity Indicator
    //                                  .scaledToFill()
    //                                  .frame(width:self.reader.size.width, height: self.reader.size.height, alignment: .center)
    //                                  .background(Color.red)
                    }
                }
            }.onAppear {
                UIScrollView.appearance().isPagingEnabled = true
            }
        }
    }
    

    这不依赖于屏幕边界,而是使用 GeometryReader 来获取包含滚动视图的大小。

    还有一点要记住的是,所有的矩形/图像都将立即创建,没有滚动视图的缓存,所以如果你有很多可能会影响性能。

    【讨论】:

    • 感谢您的回答,但我必须显示图像而不是静态颜色,所以这不起作用。最多有 4 张图片,所以我认为这应该会产生性能问题。
    • 没有什么能阻止你使用图像我只是使用静态颜色作为示例,就像你使用 Rectangle() 作为示例一样。
    【解决方案2】:

    这似乎是 Xcode 11.5 本身的问题,因为此代码现在可以正常工作。

    ScrollView(Axis.Set.horizontal, showsIndicators: false) {
                    HStack(spacing: 0 ) {
                        ForEach(self.images, id: \.self){ imageURL in
                            WebImage(url: URL.init(string: imageURL))
                                .scaledToFit()
                                .frame(width: proxy.size.width, height: proxy.size.height, alignment: .center)
                                .clipped()
                                .background(Color.white)
                        }
                    }
                }
    

    【讨论】:

    • 您仍然有可能遇到与您的代理基于UIScreen.main.bounds 相同的问题,并且由于状态栏、导航栏(如果您使用一个)等。将 SwiftUI 视图中的任何内容直接基于 UIScreen.main.bounds 并不是一个好主意
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多