如何在swiftui中打开相机和照片库

Today's life mobile cameras are playing a very important role in almost every application. Some examples are Updating profile Pics, Sending Photos, Scanning PDFs, etc. So, for an iOS developer, it is very basic to know how to integrate the camera with an iOS app.

当今生活中的移动相机在几乎所有应用中都扮演着非常重要的角色。 例如,更新个人资料图片,发送照片,扫描PDF等。因此,对于iOS开发人员而言,了解如何将相机与iOS应用集成是非常基本的。

Let’s learn how to integrate Camera in an iOS app.

让我们学习如何将Camera集成到iOS应用中。

As there is no native SwiftUI method to open Camera, we are going to use UIKit’s UIImagePickerController and wraps it in UIViewControllerRepresentable struct. Then we can use that struct as SwiftUI view.

由于没有本机的SwiftUI方法可打开Camera,因此我们将使用UIKit的UIImagePickerController并将其包装在UIViewControllerRepresentable结构中。 然后我们可以将该结构用作SwiftUI视图。

Note: Camera only works on a real iOS device.

注意:相机只能在真实的iOS设备上使用。

The process has 4 steps,

该过程分为4个步骤,

  1. Add camera permission in info.plist file

    在info.plist文件中添加相机权限
  2. Create a struct which confirms UIViewControllerRepresentable

    创建一个确认UIViewControllerRepresentable的结构
  3. Create a Coordinator class for fetching the captured image from Camera

    创建一个Coordinator类,用于从Camera中获取捕获的图像
  4. Display the image in Content view

    在内容视图中显示图像

在info.plist文件中添加相机权限 (Add camera permission in info.plist file)

Open info.plist file → Click + button → Select “Privacy — Camera Usage Description” → Enter description message for camera permission alert

打开info.plist文件→单击+按钮→选择“隐私-摄像机使用说明”→输入摄像机权限警报的说明消息

Image for post
Af
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值