UIViewControllerRepresentable从ViewController接收图像。该图像应可用于SwiftUI视图。
ObservableObject:
class CurrentImage: ObservableObject {
var didChange = PassthroughSubject<Void, Never>()
@Published var photoTaken: UIImage? = nil {didSet{didChange.send()}}
}
UIViewControllerRepresentable:
struct CameraPreviewView: UIViewControllerRepresentable {
var currentImage: CurrentImage
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject {
let parent: CameraPreviewView
init(_ cameraPreviewController: CameraPreviewView) {
self.parent = cameraPreviewController
super.init()
// Notification receives a copy of the image taken from the ViewController:
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "imageDidChange"), object: nil, queue: OperationQueue.main){(notification) in
if let image = notification.userInfo?["image"] as? UIImage {
// *****ERROR: self.parent.videoStatus.photoTaken is not accessible
self.parent.currentImage.photoTaken = image
}
}
}
}
func makeUIViewController(context: UIViewControllerRepresentableContext<CameraPreviewView>) -> CameraViewController {
let cameraViewController = CameraViewController()
return cameraViewController
}
// View is not updating so we don't need this
func updateUIViewController(_ cameraViewController: CameraViewController, context: UIViewControllerRepresentableContext<CameraPreviewView>) {
}
}
ObservableObject是“不可访问的”,因此我无法通过协调器在其中设置图像。我在这里忽略了一些简单的事情吗?
更新2:传递值到ObservableObject仍然无法正常工作
我为此苦苦挣扎,并尝试了十几种方法。但是为了简化问题,我们有一个带有表示ObservableObject的var的协调器。在这种情况下,我将使其保持简单,并尝试设置字符串的值。
协调员:
struct CameraPreviewView: UIViewControllerRepresentable {
var cameraPhoto = CurrentPhoto()
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject {
let parent: CameraPreviewView
init(_ cameraPreviewController: CameraPreviewView) {
self.parent = cameraPreviewController
super.init()
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "imageDidChange"), object: nil, queue: OperationQueue.main){(notification) in
print("notice received")
self.parent.cameraPhoto.testString = "This changed"
}
}
}
}...
ObservableObject:
class CurrentPhoto: ObservableObject {
@Published var testString = "Testing string"
}
为了对此进行测试,我还尝试了以下方法:
class CurrentPhoto: ObservableObject {
let objectWillChange = ObservableObjectPublisher()
var testString = "Testing String" {
willSet {
print("set string")
objectWillChange.send()
}
}
}
在第二个示例中,将调用willSet方法。 (它显示“设置字符串”),但是该值仍为“ Testing String”。
从控制器调用了ObservableObject IS,但是该值未设置或更改。
因此,该视图不会收到新值:
struct ContentView: View {
@ObservedObject var cameraState = CurrentPhoto()
var body: some View {
VStack{
Text("test this \(cameraState.testString)")
...
注意:将值从VIEW传递到ObservableObject是可行的,因此问题与协调器如何将值传递到ObservedObject有关。
以下工作并将一个值从View传递给OO:
Button(action : {
self.cameraState.testString = "from view"
}, label : {
Text("Change string")
}
最佳答案
这不能完全解决如何将协调器绑定到ObservableObject的问题。但是,这确实达到了预期的目标,即将图像从协调器返回到SwiftUI。
风景:
struct ContentView: View {
@State private var photoInView: Image?
@State var isRecording = true
var body: some View {
VStack{
photoInView?
.resizable()
.scaledToFit()
ZStack (alignment: .bottom) {
// Start up the CameraPreview and this will 2-way bind.
CameraPreviewView(isRecording: $isRecording, photoReceivedFromCoordinator: $photoInView)
....
然后在UIViewRepresentable中:
struct CameraPreviewView: UIViewControllerRepresentable {
@Binding var isRecording: Bool
@Binding var photoReceivedFromCoordinator: Image?
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject {
// Setting the coordinator to parent allows the coordinator to use the @Binding
let parent: CameraPreviewView
init(_ cameraPreviewController: CameraPreviewView) {
self.parent = cameraPreviewController
super.init()
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "imageDidChange"), object: nil, queue: OperationQueue.main){(notification) in
if let uiImage = notification.userInfo?["image"] as? UIImage {
// We need an Image not a UI Image, so first we convert it
let image = Image(uiImage: uiImage)
// Assign the value to the parent's binding value
self.parent.photoReceivedFromCoordinator = image
}
}
}
}
关于swift - SwiftUI-协调器的ObservableObject更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59108195/