您的位置:首页 > 移动开发 > Swift

swiftui_SwiftUI:如何使用PHPicker(PhotosUI)从库中选择图像?

2020-07-26 09:21 1236 查看

swiftui

With WWDC20, Apple added a new framework named PhotosUI to select images, videos etc from Library. In this tutorial, we going to implement the same with SwiftUI.

借助WWDC20,Apple添加了一个名为PhotosUI的新框架,以从Library中选择图像,视频等。 在本教程中,我们将使用SwiftUI来实现相同的功能。

As PhotosUI is based on UIKit not SwiftUI, We need to write a representable. this representable can use PhotosUI’s “PHPickerViewController” in SwiftUI.

由于PhotosUI是基于UIKit而不是SwiftUI的,因此我们需要编写一个可表示的对象。 这个可表示的对象可以在SwiftUI中使用PhotosUI的“ PHPickerViewController ”。

Picker 选择器
  • PHPickerViewController needs a PHPickerConfiguration as initialiser.

    PHPickerViewController需要一个PHPickerConfiguration作为初始化程序。

  • PhotoPicker will need toggle state to dismiss controller once user is done

    用户完成操作后,PhotoPicker将需要切换状态以关闭控制器
  • PhotoPicker will also need a binging object to pass on Images back to caller.

    PhotoPicker还需要绑定对象,以将图像传回给调用者。
  • Implement PHPickerViewControllerDelegate to handle return Image from Library

    实现PHPickerViewControllerDelegate以处理来自库的返回图像

创建PHPickerConfiguration并使用绑定对象切换状态: (Creating PHPickerConfiguration and toggle state with binding object:)

With above code, We can present Picker. Now what left is delegate handling.

通过上面的代码,我们可以展示Picker。 现在剩下的就是委托处理了。

Implement PHPickerViewControllerDelegate, we are going to create Coordinator with PHPickerViewControllerDelegate. this coordinator will handle callback and binding of data like following :

实现PHPickerViewControllerDelegate,我们将使用PHPickerViewControllerDelegate创建协调器。 该协调器将处理回调和数据绑定,如下所示:

Let’s combine everything:

让我们结合一切:

A working Demo 工作中的演示 UI With selected Image 具有所选图像的UI

翻译自: https://medium.com/dev-genius/swiftui-how-to-use-phpicker-photosui-to-select-image-from-library-5b74885720ec

swiftui

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: