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个步骤,
- Add camera permission in info.plist file 在info.plist文件中添加相机权限
- Create a struct which confirms UIViewControllerRepresentable 创建一个确认UIViewControllerRepresentable的结构
- Create a Coordinator class for fetching the captured image from Camera 创建一个Coordinator类,用于从Camera中获取捕获的图像
- 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文件→单击+按钮→选择“隐私-摄像机使用说明”→输入摄像机权限警报的说明消息