繁体   English   中英

SwiftUI:在 NavigationLink 中将图像用作 label 时使用率高 memory

[英]SwiftUI: High memory usage when using Image as label in NavigationLink

我想在 NavigationLink 上显示使用 Image 作为 label 的菜单,但是当我在真实设备和模拟器上运行应用程序时。 The memory usage on debug session shows around 400MB, but when I remove the image and change them to text, the memory usage shows only around 20MB and if I change all label with the same image, the memory usage shows around 80MB. 发生什么事?? 我想在我的 NavigationLink 上将图像用作 label 而不使用很多 memory 用法,知道怎么做吗?

这是代码

struct HomeMenu: View{
    var body: some View{
        GeometryReader{ geometry in
            ScrollView(showsIndicators: false){
                HStack{
                    NavigationLink(destination: ChooseServiceLocationView()){
                        MainMenu(image: "service_location_new_final")
                            .frame(width: geometry.size.width * 0.44, height: geometry.size.height * 0.66)
                    }
                    NavigationLink(destination: BookingServiceView()){
                        MainMenu(image: "booking_service_new_final")
                            .frame(width: geometry.size.width * 0.44, height: geometry.size.height * 0.66)
                    }
                }
                HStack{
                    NavigationLink(destination: DealerLocationView()){
                        MainMenu(image: "dealer_location_new_final")
                            .frame(width: geometry.size.width * 0.44, height: geometry.size.height * 0.66)
                    }
                    NavigationLink(destination: BookTestDriveView()){
                        MainMenu(image: "booking_test_drive_new_final")
                            .frame(width: geometry.size.width * 0.44, height: geometry.size.height * 0.66)  
                    }
                }
                HStack{
                    NavigationLink(destination: VehicleLineUpView()){
                        MainMenu(image: "vehicle_line_up_new_final")
                            .frame(width: geometry.size.width * 0.44, height: geometry.size.height * 0.66)
                    }
                    NavigationLink(destination: SellMyCarView()){
                        MainMenu(image: "sell_my_car_new_final")
                            .frame(width: geometry.size.width * 0.44, height: geometry.size.height * 0.66)
                    }
                }
            }
        }
    }
}

struct MainMenu: View{
    var image: String
    var body: some View{
        VStack{
            Image(image)
                .resizable()
                .scaledToFit()
        }
    }
}

发生这种情况是因为 label 是一个变量,而不是一个块,因此即使不需要显示它,也会调用标签的主体。

对于这种情况,您可以查看惰性视图包装器

public struct LazyView<Content: View>: View {
    private let build: () -> Content
    public init(_ build: @autoclosure @escaping () -> Content) {
        self.build = build
    }
    public var body: Content {
        build()
    }
}

并像这样使用它:

NavigationLink(destination: LazyView(VehicleLineUpView())){
    MainMenu(image: "vehicle_line_up_new_final")
        .frame(width: geometry.size.width * 0.44, height: geometry.size.height * 0.66)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM