2

我有一个 SwiftUI 应用程序,它使用了一个图像选择器,该图像选择器UIViewControllerRepresentable使用.sheet修饰符来显示它。

选择图像后,应用程序的其余部分或多或少会停止运行,直到后台出现某种崩溃,然后一切又变得很快 - 代码后有更多信息:

import SwiftUI

struct StylePanel: View {
    
    @Binding var image: Image?

    @State var inputImage: UIImage? = nil
    @State var showImagePicker: Bool

    func loadImage() {
        guard let inputImage = inputImage else { return }
        image = Image(uiImage: inputImage)
    }
    
    var body: some View {
        
        VStack {
            // Rest of view not shown


            Button(action: {
                self.showImagePicker = true
            }, label: {
                Text("Choose image...")
            })
        }
        .sheet(isPresented: self.$showImagePicker, onDismiss: loadImage){
            ImagePicker(image: self.$inputImage)
        }
    }
}

ImagePicker.swift

import SwiftUI

struct ImagePicker: UIViewControllerRepresentable {
    @Environment(\.presentationMode) var presentationMode
    @Binding var image: UIImage?

    func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
        let picker = UIImagePickerController()
        picker.delegate = context.coordinator
        return picker
    }

    func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<ImagePicker>) {

    }
        
    func makeCoordinator() -> ImagePickerCoordinator {
        return ImagePickerCoordinator(self)
    }
}


class ImagePickerCoordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
    let parent: ImagePicker

    init(_ parent: ImagePicker) {
        self.parent = parent
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
        if let uiImage = info[.originalImage] as? UIImage {
            parent.image = uiImage
        }

        parent.presentationMode.wrappedValue.dismiss()
    }
}

选择图像后,它会在父级中正常显示,但其他操作(例如拖动或修改状态)确实很麻烦。该应用程序非常无响应,直到最终发生了一些奇怪的事情——后台发生了某种viewServiceDidTerminateWithError崩溃,一切又开始完美运行,性能恢复正常。随后重新加载图像选择器会使情况变得更糟,直到发生同样的事情——背景崩溃,然后是正常性能。

这是性能恢复正常后的错误:

[ServicesDaemonManager] interruptionHandler is called. -[FontServicesDaemonManager connection]_block_invoke
[xpc] XPC error talking to pkd: Connection interrupted
[lifecycle] [u 123223B6-8A6C-4BC3-9908-269FA8A8B9E6:m (null)] [com.apple.mobileslideshow.photo-picker(1.0)] Connection to plugin interrupted while in use.
[lifecycle] [u 123223B6-8A6C-4BC3-9908-269FA8A8B9E6:m (null)] [com.apple.mobileslideshow.photo-picker(1.0)] Connection to plugin invalidated while in use.
viewServiceDidTerminateWithError:: Error Domain=_UIViewServiceInterfaceErrorDomain Code=3 "(null)" UserInfo={Message=Service Connection Interrupted}
[Generic] -[PUPhotoPickerHostViewController viewServiceDidTerminateWithError:] Error Error Domain=_UIViewServiceInterfaceErrorDomain Code=3 "(null)" UserInfo={Message=Service Connection Interrupted}

它似乎确实与使用.sheet. 当我将 Picker 直接放在包含视图中时,它可以正常工作,没有任何问题。

我想知道的是如何正确处理UIImagePickerController模态以阻止此性能问题。

有没有其他人经历过这个,或者知道解决方案?

4

1 回答 1

0

您是否尝试过使用@StateObjectvar inputImage 而不是@State?这将初始化图像一次,避免在视图更改时不断重新加载图像。

于 2020-11-23T18:32:33.977 回答