首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用UIImagePickerController后的性能问题

使用UIImagePickerController后的性能问题
EN

Stack Overflow用户
提问于 2020-10-26 00:31:06
回答 1查看 261关注 0票数 2

我有一个SwiftUI应用程序,它使用包装在UIViewControllerRepresentable中的图像选择器,使用.sheet修饰符来显示它。

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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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崩溃,一切都开始完美地运行起来,性能恢复正常。随后图像拾取器的重新加载使情况变得更糟,直到同样的事情发生-后台崩溃,然后正常运行。

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

代码语言:javascript
运行
复制
[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模式来解决这个性能问题。

还有没有其他人经历过这种情况,或者知道解决方案?

EN

回答 1

Stack Overflow用户

发布于 2020-11-24 02:32:34

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64526094

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档