UIViewRepresentable @state值未更新

时间:2020-07-14 02:23:46

标签: swiftui

我有一个要封装UIImage的UIViewRepresentable。

import SwiftUI

struct ImageView: UIViewRepresentable {
    
    var name: String
    
    fileprivate var imageView: UIImageView = UIImageView()
    fileprivate var originalImage: UIImage
    @State fileprivate var size: CGSize
    

    init(name: String) {
        self.name = name
        self.originalImage = UIImage(named: name)!
        self._size = State(initialValue: self.originalImage.size)
        print(111, self.size)
    }
    
    func makeUIView(context: Context) -> UIImageView {
        imageView.image = self.originalImage

        return imageView;
    }
    
    func updateUIView(_ uiView: UIImageView, context: Context) {
        self.imageView.image = scaledImage()
    }
    
    fileprivate func scaledImage() -> UIImage {
        print(333, self.size)
        if (self.originalImage.size == self.size || self.size.width <= CGFloat(1) || self.size.height <= CGFloat(1)) {
            return self.originalImage
        }
        
        UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
        self.originalImage.draw(in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return image!;
    }
    
    func resize(width: CGFloat, height: CGFloat) -> some View {
        self.size = CGSize(width: width, height: height)
        print(222, self.size, width, height)
        
        return self.frame(width: width, height: height)
    }
}

问题是当调用.resize时,size状态不会更新。这是3条打印线:

111 (2000.0, 2000.0)
222 (2000.0, 2000.0) 100.0 100.0
333 (2000.0, 2000.0)

这是使用View ...的测试ImageView ...

struct Test: View {
    var body: some View {
        ZStack {
            ImageView(name: "Letter-T")
            .resize(width: 100, height: 100)
        }
    }
}

我在做什么错??

1 个答案:

答案 0 :(得分:0)

也许这是您想要的吗?

struct ContentView: View {
    @State private var size = CGSize(width: 1000, height: 1000)
    
    var body: some View {
            ZStack {
                ImageView(name: "letter-t", size: $size)
            }.onAppear {
                self.size = CGSize(width: 100, height: 100)
            }
        }
}

struct ImageView: UIViewRepresentable {
    
    var name: String
    
    fileprivate var imageView: UIImageView = UIImageView()
    fileprivate var originalImage: UIImage
    fileprivate var size: Binding<CGSize>
    
    init(name: String, size: Binding<CGSize>) {
        self.name = name
        self.originalImage = UIImage(named: name)!
        self.size = size
        print(111, self.size.wrappedValue)
    }
    
    func makeUIView(context: Context) -> UIImageView {
        imageView.image = self.originalImage

        return imageView;
    }
    
    func updateUIView(_ uiView: UIImageView, context: Context) {
        self.imageView.image = scaledImage()
    }
    
    fileprivate func scaledImage() -> UIImage {
        print(333, self.size.wrappedValue)
        if (self.originalImage.size == self.size.wrappedValue || self.size.wrappedValue.width <= CGFloat(1) || self.size.wrappedValue.height <= CGFloat(1)) {
            return self.originalImage
        }
        
        UIGraphicsBeginImageContextWithOptions(size.wrappedValue, false, 0.0)
        self.originalImage.draw(in: CGRect(x: 0, y: 0, width: self.size.wrappedValue.width, height: self.size.wrappedValue.height))
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return image!;
    }
}

我不得不说,在@State中使用UIViewRepresentable很奇怪,没有在任何地方看到它,也不认为它有用。