如何从 SwiftUI 调用 PHPhotoLibrary presentLimitedLibraryPicker?

How to call PHPhotoLibrary presentLimitedLibraryPicker from SwiftUI?

从 SwiftUI 调用 PHPhotoLibrary 的 presentLimitedLibraryPicker 方法的正确方法是什么?

该方法需要一个 UIViewController,我在 SwiftUI 中没有。

我尝试使用 UIViewControllerRepresentable 来创建一个 UIViewController,它确实有效,但结果是出现了两个视图控制器,我用 UI 创建的那个ViewController可代表的,和有限的图书馆选择器。

两个视图控制器都需要关闭才能到达原始屏幕,这是不可取的。

总结一下我看到的问题:

  1. presentLimitedLibraryPicker 通过传递 ViewController.
  2. 来工作
  3. 这让我创建并呈现一个虚拟对象 ViewController,这样我就可以调用该方法。
  4. 无法获取对有限库选择器的引用,它也不提供委托。所以我无法检测到 Limited Library Picker 控制器何时被关闭。

这是我的尝试(它显示了选择器,但是当你关闭它时,你也需要关闭额外的虚拟视图控制器:

import Foundation
import SwiftUI
import Photos
import PhotosUI

struct TestView: View {

   @State var showLibraryPicker = false

var body: some View {
    NavigationView {
        VStack {
            Button("Open Library Picker") { showLibraryPicker = true }
        }
        .navigationBarTitle("Test", displayMode: .inline)
        .navigationViewStyle(StackNavigationViewStyle())
        .sheet(isPresented: $showLibraryPicker, onDismiss: { print("Dismissed") }) {
            TestLimitedLibraryPicker()
            }
        }
    }
}


struct TestLimitedLibraryPicker: UIViewControllerRepresentable {

     func makeUIViewController(context: Context) -> UIViewController {
         let controller = UIViewController()
         PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: controller)
          return controller
     }

    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {

     }
}

下面是一些演示方法。使用 Xcode 12 / iOS 14.

准备和测试
struct TestPhotosView: View {

    @State var showLibraryPicker = false

    var body: some View {
        NavigationView {
            VStack {
                Button("Open Library Picker") { self.showLibraryPicker = true }
            }
            .navigationBarTitle("Test", displayMode: .inline)
            .navigationViewStyle(StackNavigationViewStyle())
            .background(Group {
                if self.showLibraryPicker {
                    TestLimitedLibraryPicker(isPresented: $showLibraryPicker)
                }
            })
        }
    }
}


struct TestLimitedLibraryPicker: UIViewControllerRepresentable {
    @Binding var isPresented: Bool

    func makeUIViewController(context: Context) -> UIViewController {
        let controller = UIViewController()

        DispatchQueue.main.async {
            PHPhotoLibrary.requestAuthorization() { result in
                PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: controller)
                context.coordinator.trackCompletion(in: controller)
            }
        }
        
        return controller
    }

    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}

    func makeCoordinator() -> Coordinator {
        Coordinator(isPresented: $isPresented)
    }
    class Coordinator: NSObject {
        private var isPresented: Binding<Bool>
        init(isPresented: Binding<Bool>) {
            self.isPresented = isPresented
        }

        func trackCompletion(in controller: UIViewController) {
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self, weak controller] in
                if controller?.presentedViewController == nil {
                    self?.isPresented.wrappedValue = false
                } else if let controller = controller {
                    self?.trackCompletion(in: controller)
                }
            }
        }
    }
}

backup

刚遇到这个问题!这是我的看法。

  • 没有 SwiftUI 方法来调用有限的照片选择器。所以我们必须创建一个调用 .presentLimitedLibraryPicker 的视图控制器 (UIViewControllerRepresentable)。但是,如果您将其显示为 sheet,它将立即显示选择器,并且您将在 sheet.
  • 上获得 sheet
  • 相反,将包装的 View Controller 直接嵌入 View 的主体并将其传递给条件触发器的绑定。
  • 在包装的视图控制器中,查找 updateUIViewController 中条件的更改。如果值为 true,显示选择器,然后立即关闭该值。
import SwiftUI
import UIKit
import PhotosUI

struct ContainingView: View {
    @State var showLimitedPicker: Bool = false

    var body: some View {
        HStack {
            Button("Load more…") { showLimitedPicker = true }
            LimitedPicker(isPresented: $showLimitedPicker)
                .frame(width: 0, height: 0)
        }
    }
}

struct LimitedPicker: UIViewControllerRepresentable {
    @Binding var isPresented: Bool

    func makeUIViewController(context: Context) -> UIViewController {
        UIViewController()
    }

    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
        if isPresented {
            PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: uiViewController)
            DispatchQueue.main.async {
                isPresented = false
            }
        }
    }
}