2

我有一个奇怪的问题,如果我制作的可调整大小的图像(自定义视图)不在 NavigationButton 中,那么它可以正常工作,但如果它在其中,那么图像的大小仍然很好,但它变成了全蓝色。有谁知道出了什么问题?

struct DynamicallyResizableImage: View {
    let imageName: String
    let height: CGFloat
    let width: CGFloat

    init(imageName: String, height: CGFloat? = nil, width: CGFloat? = nil) {
        self.imageName = imageName
        let imageSize = UIImage(named: imageName)!.size

        guard let height = height else {
            if let width = width {

                self.width = width
                self.height = imageSize.height * (width/imageSize.width)
                return
            }
            self.height = 0
            self.width = 0
            return
        }

        if let width = width {
            self.height = height
            self.width = width
            return
        }

        self.height = height
        self.width = imageSize.width * (height/imageSize.height)
    }

    var body: some View {
        Image(imageName)
            .resizable()
            .frame(width: width, height: height)
    }

}
NavigationButton(destination: PicturePreviewView(imageName: "IMG_4955")) {
                        DynamicallyResizableImage(imageName: "IMG_4955", height: 200)
                    }

当未嵌入 NavigationButton 时,一切正常。当嵌入 NavigationButton 时,图像会被蓝色填充。

任何人都知道如何解决它?谢谢!!!

4

1 回答 1

1

所以我在一位redditor同事的帮助下修复了它。在自定义视图内部,我必须像这样向 Image 添加一个 renderingMode:

Image(imageName)
            .renderingMode(.original)
            .resizable()
            .frame(width: width, height: height)
于 2019-06-18T08:45:26.580 回答