PHPickerViewController 点击搜索时出现错误...“无法加载照片”

Fog*_*ter 11 ios swiftui phpickerviewcontroller

我正在尝试PHPickerViewController使用 SwiftUI 和可组合架构来实现。(并不是说我认为这特别相关,但它可以解释为什么我的一些代码是这样的)。

示例项目

我一直在尝试解决这个问题。我在 GitHub 上创建了一个小示例项目,它删除了可组合架构并保持 UI 超级简单。

https://github.com/oliverfoggin/BrokenImagePickers/tree/main

看起来 iOS 15 在 UIImagePickerViewController 和 PHPickerViewController 上都出现了问题。(这是有道理的,因为它们都在后台使用相同的 UI)。

我猜接下来的步骤是确定在 UIKit 应用程序中使用它们时是否会发生相同的错误。

我的代码

我的代码相当简单。它几乎只是使用相同功能的重新实现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))
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

所有这些都适用于简单的情况

我可以展示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}
Run Code Online (Sandbox Code Playgroud)

点击“重试”按钮会重新加载初始滚动屏幕,我可以继续使用它。但再次点击搜索栏只会显示相同的错误。

我通常是第一个指出该错误几乎肯定与 Apple API 无关的人,但我对这个问题感到困惑。我不确定我正在做的事情是什么导致了这种情况的发生?

它是在 SwiftUI 视图中吗?

在 UIKit 中重新创建项目

我使用 UIKit 重新制作了相同的项目... https://github.com/oliverfoggin/UIKit-Image-Pickers

我根本无法复制崩溃的情况。

另外...如果您对设备进行任何类型的屏幕录制,则不会发生崩溃。我尝试在设备本身上进行录音,但无法复制。我还尝试使用 iPhone 屏幕在 Mac 上录制电影,但无法重现崩溃情况。但是...当我停止 QuickTime 上的录制时,崩溃又再次出现了。

F22*_*ing 8

这为我解决了.ignoreSafeArea(.keyboard)这个问题,就像@Frusterated_Student 提到的那样。

为了详细说明@Frusterated_Student,这个问题与UIViewControllerRepresentable像许多 SwiftUI 视图一样处理视图以自动避免键盘有关。如果您像我一样使用工作表来呈现选择器,那么您可以简单地添加到.ignoreSafeArea(.keyboard)视图UIViewControllerRepresentable中,在我的例子中,我称之为ImagePicker这里是一个更好的例子。

在哪里添加.ignoreSafeArea(.keyboard)

.sheet(isPresented: $imagePicker) {
    ImagePicker(store: store)
        .ignoresSafeArea(.keyboard)
}
Run Code Online (Sandbox Code Playgroud)

这是@Fogmeister 代码:

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))
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


Fog*_*ter 5

嗯..这似乎是一个 iOS 错误。

我在这里创建了一个示例项目来显示该错误... https://github.com/oliverfoggin/BrokenImagePickers

这里用 UIKit 编写的复制项目不... https://github.com/oliverfoggin/UIKit-Image-Pickers

我尝试对发生的情况进行屏幕录制,但似乎如果发生任何屏幕录制(无论是在设备上还是通过 Mac 上的 QuickTime),这都会抑制错误的发生。

我已经向苹果公司提交了一份雷达报告,并向他们发送了两个项目以供他们查看,并提供了有关正在发生的事情的大量详细信息。我将随时更新这方面的任何进展。

黑客解决方法

经过进一步调查后,我发现您可以从 SwiftUI 开始,然后呈现 PHPickerViewController 而不会发生此崩溃。

从 SwiftUI 中,如果您提供 UIViewControllerRepresentable...,然后从那里如果您提供 PHPickerViewController 它不会崩溃。

所以我想出了一个(非常俗气的)解决方法来避免这种崩溃。

我首先创建一个UIViewController子类,将其用作包装器。

class WrappedPhotoPicker: UIViewController {
  var picker: PHPickerViewController?
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    if let picker = picker {
      present(picker, animated: false)
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然后在 SwiftUI 视图中,我创建这个包装器并在其中设置选择器。

struct WrappedPickerView: UIViewControllerRepresentable {
  @Environment(\.presentationMode) var presentationMode
  @Binding var photoPickerResult: PHPickerResult?
  
  let wrappedPicker = WrappedPhotoPicker()
  
  func makeUIViewController(context: Context) -> WrappedPhotoPicker {
    var config = PHPickerConfiguration()
    config.filter = .images
    config.selectionLimit = 1
    
    let picker = PHPickerViewController(configuration: config)
    picker.delegate = context.coordinator
    
    wrappedPicker.picker = picker
    return wrappedPicker
  }
  
  func updateUIViewController(_ uiViewController: WrappedPhotoPicker, context: Context) {}
  
  func makeCoordinator() -> Coordinator {
    Coordinator(self)
  }
  
  class Coordinator: PHPickerViewControllerDelegate {
    let parent: WrappedPickerView
    
    init(_ parent: WrappedPickerView) {
      self.parent = parent
    }
    
    func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
      parent.presentationMode.wrappedValue.dismiss()
      parent.wrappedPicker.dismiss(animated: false)
      
      parent.photoPickerResult = results.first
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这远非理想,因为我在错误的时间和地点进行了演示。但在苹果提供永久解决方案之前,它会一直有效。