我正在尝试实现一个PHPickerViewController
使用 SwiftUI 和可组合架构。 (并不是说我认为这特别相关,但它可以解释为什么我的一些代码是这样的)。
示例项目
我一直在尝试解决这个问题。我在 GitHub 上创建了一个小示例项目,它删除了可组合架构并保持 UI 超级简单。
https://github.com/oliverfoggin/BrokenImagePickers/tree/main https://github.com/oliverfoggin/BrokenImagePickers/tree/main
看起来 iOS 15 在 UIImagePickerViewController 和 PHPickerViewController 上都出现了问题。 (这是有道理的,因为它们都在后台使用相同的 UI)。
我猜接下来的步骤是确定在 UIKit 应用程序中使用它们时是否会发生相同的错误。
My code
我的代码相当简单。它几乎只是使用相同功能的重新实现UIImagePickerViewController
但我想尝试使用较新的 API。
我的代码看起来像这样......
public struct ImagePicker: UIViewControllerRepresentable {
// Vars and setup stuff...
@Environment(\.presentationMode) var presentationMode
let viewStore: ViewStore<ImagePickerState, ImagePickerAction>
public init(store: Store<ImagePickerState, ImagePickerAction>) {
self.viewStore = ViewStore(store)
}
// UIViewControllerRepresentable required functions
public func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> some UIViewController {
// Configuring the PHPickerViewController
var config = PHPickerConfiguration()
config.filter = PHPickerFilter.images
let picker = PHPickerViewController(configuration: config)
picker.delegate = context.coordinator
return picker
}
public func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {}
public func makeCoordinator() -> Coordinator {
Coordinator(self)
}
// This is the coordinator that acts as the delegate
public class Coordinator: PHPickerViewControllerDelegate {
let parent: ImagePicker
init(_ parent: ImagePicker) {
self.parent = parent
}
public func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
picker.dismiss(animated: true)
guard let itemProvider = results.first?.itemProvider,
itemProvider.canLoadObject(ofClass: UIImage.self) else {
return
}
itemProvider.loadObject(ofClass: UIImage.self) { [weak self] image, error in
if let image = image as? UIImage {
DispatchQueue.main.async {
self?.parent.viewStore.send(.imagePicked(image: image))
}
}
}
}
}
}
所有这些都适用于简单的情况
我可以介绍ImagePicker
查看并选择一张照片,一切都很好。我可以取消它,好吧。我什至可以向下滚动我拥有的巨大图像集合视图。我什至可以看到新图像出现在我的状态对象中并将其显示在我的应用程序中。 (注意...这仍然是 WIP,所以代码有点笨拙,但这只是为了让它最初工作)。
问题案例
问题是,当我点击搜索栏时PHPickerView
(这是Apple在控件中提供的搜索栏,我没有创建它或编码它)。它似乎开始向上滑动键盘,然后视图变为空白,中间有一条消息......
无法加载照片
[再试一次]
我还收到一个看起来很奇怪的错误日志。 (我删除了时间戳以缩短线路)。
// These happen on immediately presenting the ImagePicker
AppName[587:30596] [Picker] Showing picker unavailable UI (reason: still loading) with error: (null)
AppName[587:30596] Writing analzed variants.
// These happen when tapping the search bar
AppName[587:30867] [lifecycle] [u A95D90FC-C77B-43CC-8FC6-C8E7C81DD22A:m (null)] [com.apple.mobileslideshow.photospicker(1.0)] Connection to plugin interrupted while in use.
AppName[587:31002] [lifecycle] [u A95D90FC-C77B-43CC-8FC6-C8E7C81DD22A:m (null)] [com.apple.mobileslideshow.photospicker(1.0)] Connection to plugin invalidated while in use.
AppName[587:30596] [Picker] Showing picker unavailable UI (reason: crashed) with error: (null)
AppName[587:30596] viewServiceDidTerminateWithError:: Error Domain=_UIViewServiceInterfaceErrorDomain Code=3 "(null)" UserInfo={Message=Service Connection Interrupted}
点击“重试”按钮会重新加载初始滚动屏幕,我可以继续使用它。但再次点击搜索栏只会显示相同的错误。
我通常是第一个指出错误几乎肯定不是由 Apple API 引起的,但我对这个问题感到困惑。我不确定我正在做的是什么导致了这种情况的发生?
它是在 SwiftUI 视图中吗?
在 UIKit 中重新创建项目
我使用 UIKit 重新制作了同一个项目......https://github.com/oliverfoggin/UIKit-Image-Pickers https://github.com/oliverfoggin/UIKit-Image-Pickers
我根本无法复制崩溃的情况。
另外...如果您对设备进行任何类型的屏幕录制,则不会发生崩溃。我尝试在设备本身上进行录音,但无法复制。我还尝试使用 iPhone 屏幕在 Mac 上录制电影,但无法重现崩溃情况。但是...当我停止 QuickTime 上的录制时,崩溃又可以重现。