如何解决使用UIImagePickerController
我有一个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
的使用直接相关。当我将选择器直接放在包含视图中时,它可以正常工作而没有任何问题。
我想知道的是如何正确处置UIImagePickerController
模式以停止此性能问题。
其他人有没有经历过或知道解决方案?
解决方法
您是否尝试过使用@StateObject
var inputImage而不是@State
?这样将对图像进行一次初始化,从而避免在视图更改时不断重新加载图像。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。