SwiftUI调用相机拍照

news/2024/9/23 21:15:54/

在 SwiftUI 中实现拍照功能,需要结合 UIViewControllerRepresentableUIImagePickerController 来实现相机功能。下面是一个详细的示例,展示如何使用 SwiftUI 来实现拍照功能:

1. 创建一个 ImagePicker 组件

首先,创建一个 UIViewControllerRepresentable 结构,用于包装 UIImagePickerController

import SwiftUI
import UIKitstruct ImagePicker: UIViewControllerRepresentable {@Binding var selectedImage: UIImage?@Environment(\.presentationMode) var presentationModevar sourceType: UIImagePickerController.SourceType = .cameraclass Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {let parent: ImagePickerinit(parent: ImagePicker) {self.parent = parent}func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {if let image = info[.originalImage] as? UIImage {parent.selectedImage = image}parent.presentationMode.wrappedValue.dismiss()}func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {parent.presentationMode.wrappedValue.dismiss()}}func makeCoordinator() -> Coordinator {Coordinator(parent: self)}func makeUIViewController(context: Context) -> UIImagePickerController {let picker = UIImagePickerController()picker.delegate = context.coordinatorpicker.sourceType = sourceTypereturn picker}func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {}
}

2. 使用 ImagePicker 组件

接下来,在你的主视图中使用 ImagePicker 组件来实现拍照功能。

import SwiftUIstruct ContentView: View {@State private var isImagePickerPresented = false@State private var selectedImage: UIImage?var body: some View {VStack {if let selectedImage = selectedImage {Image(uiImage: selectedImage).resizable().scaledToFit().frame(width: 300, height: 300)} else {Text("No Image Selected").frame(width: 300, height: 300).background(Color.gray)}Button(action: {isImagePickerPresented = true}) {Text("Take Photo").padding().background(Color.blue).foregroundColor(.white).cornerRadius(10)}.padding()}.sheet(isPresented: $isImagePickerPresented) {ImagePicker(selectedImage: $selectedImage)}}
}struct ContentView_Previews: PreviewProvider {static var previews: some View {ContentView()}
}

解释

  1. ImagePicker 组件

    • UIViewControllerRepresentable 协议用来将 UIImagePickerController 引入 SwiftUI。
    • makeUIViewControllerupdateUIViewController 方法创建和更新 UIImagePickerController
    • Coordinator 类作为 UIImagePickerController 的代理,处理图片选择和取消操作。
  2. ContentView

    • 使用 @State 属性包装变量 isImagePickerPresented 来控制 ImagePicker 的显示。
    • 使用 @State 属性包装变量 selectedImage 来存储选取的图片。
    • 当点击 “Take Photo” 按钮时,显示 ImagePicker
    • sheet 修饰符用于在 isImagePickerPresentedtrue 时呈现 ImagePicker

通过这种方式,你可以在 SwiftUI 应用中实现拍照功能。请注意,拍照功能只能在真实设备上使用,因为模拟器不支持摄像头。

在 iOS 应用中访问相机需要在 Info.plist 文件中添加 NSCameraUsageDescription 键,以告知用户为什么需要访问相机。否则,应用在尝试访问相机时会崩溃。

添加 NSCameraUsageDescriptionInfo.plist

  1. 打开你的 Xcode 项目。

  2. 在项目导航中,找到并点击你的 Info.plist 文件。

  3. Info.plist 中,添加一个新的键值对:

    • 键:NSCameraUsageDescription
    • 值:解释你的应用需要使用相机的原因,比如 “This app requires access to the camera to take photos.”

示例:

<key>NSCameraUsageDescription</key>
<string>This app requires access to the camera to take photos.</string>

如果你使用 Xcode 的图形化界面,可以按以下步骤操作:

  1. 打开 Info.plist 文件。
  2. 点击右键选择 “Add Row”。
  3. 在新行的键列中输入 NSCameraUsageDescription
  4. 在值列中输入对用户的说明,比如 “This app requires access to the camera to take photos.”

更新后的示例代码

完成上述步骤后,你可以重新运行之前的代码:

import SwiftUI
import UIKitstruct ImagePicker: UIViewControllerRepresentable {@Binding var selectedImage: UIImage?@Environment(\.presentationMode) var presentationModevar sourceType: UIImagePickerController.SourceType = .cameraclass Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {let parent: ImagePickerinit(parent: ImagePicker) {self.parent = parent}func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {if let image = info[.originalImage] as? UIImage {parent.selectedImage = image}parent.presentationMode.wrappedValue.dismiss()}func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {parent.presentationMode.wrappedValue.dismiss()}}func makeCoordinator() -> Coordinator {Coordinator(parent: self)}func makeUIViewController(context: Context) -> UIImagePickerController {let picker = UIImagePickerController()picker.delegate = context.coordinatorpicker.sourceType = sourceTypereturn picker}func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {}
}struct ContentView: View {@State private var isImagePickerPresented = false@State private var selectedImage: UIImage?var body: some View {VStack {if let selectedImage = selectedImage {Image(uiImage: selectedImage).resizable().scaledToFit().frame(width: 300, height: 300)} else {Text("No Image Selected").frame(width: 300, height: 300).background(Color.gray)}Button(action: {isImagePickerPresented = true}) {Text("Take Photo").padding().background(Color.blue).foregroundColor(.white).cornerRadius(10)}.padding()}.sheet(isPresented: $isImagePickerPresented) {ImagePicker(selectedImage: $selectedImage)}}
}struct ContentView_Previews: PreviewProvider {static var previews: some View {ContentView()}
}

通过添加 NSCameraUsageDescription,应用在请求访问相机时会向用户显示一条提示,解释为什么需要访问相机,从而避免因未声明权限而导致的崩溃。


http://www.ppmy.cn/news/1466867.html

相关文章

【从零开始部署SAM(Segment Anything Model )大模型 3 Ubuntu20 离线部署 C++】

这里是目录 总览环境配置模型准备Moble SAM onnx模型获取Moble SAM pre onnx模型获取 运行cmakelist 运行结果 总览 相比于使用python离线部署SAM大模型&#xff0c;C要麻烦的多&#xff0c;本篇的部署过程主要基于项目&#xff1a;https://github.com/dinglufe/segment-anyth…

如何用结构化写好GPT的Prompt提示词

背景 最早接触 Prompt engineering 时, 学到的 Prompt 技巧都是: 你是一个 XX 角色…你是一个有着 X 年经验的 XX 角色…你会 XX, 不要 YY…对于你不会的东西, 不要瞎说!… 对比什么技巧都不用, 直接像使用搜索引擎一样提问, 上面的技巧对于回复的效果确实有着 明显提升. 在看…

【Java】数据加密

目录 数据加密介绍使用场景密码学历史古代密码学凯撒密码例子特点 维吉尼亚密码原理例子特点 现代密码学介绍 现代密码学的加密算法分类哈希算法优点缺点代码示例【封装写法】 对称加密算法对称加密算法的加密过程解密过程对称加密算法的优点&#xff1a;对称加密算法的缺点&am…

C++:栈(stack)、队列(queue)、优先级队列(priority_queue)

hello&#xff0c;各位小伙伴&#xff0c;本篇文章跟大家一起学习《C&#xff1a;栈&#xff08;stack&#xff09;和队列&#xff08;queue&#xff09;》&#xff0c;感谢大家对我上一篇的支持&#xff0c;如有什么问题&#xff0c;还请多多指教 &#xff01; 文章目录 :map…

【leetcode--O(1) 时间插入、删除和获取随机元素】

这道题要求实现一个类&#xff0c;满足插入、删除和获取随机元素操作的平均时间复杂度为 O(1)。 变长数组可以在 O(1) 的时间内完成获取随机元素操作&#xff0c;但是由于无法在 O(1)的时间内判断元素是否存在&#xff0c;因此不能在 O(1) 的时间内完成插入和删除操作。哈希表…

ffmpeg 的sws_scale接口函数解析

ffmpeg 的 sws_scale 函数是 libswscale 库中的一个重要函数&#xff0c;用于进行图像的缩放和颜色空间转换。它的主要作用是将输入图像帧转换为另一种尺寸或颜色格式的输出图像帧。下面详细解析一下 sws_scale 函数的作用、参数等。 sws_scale 函数的作用 ffmpeg 的 sws_sca…

Codeforces Round 950 (Div. 3)(A~E题解)

这场比赛我自己打的是真的垃圾&#xff0c;也是侥幸被拿下了&#xff0c;第三题当时没想清楚&#xff0c;要不然还能止损一下&#xff0c;惜败惜败 话不多说&#xff0c;现在来看A~E题的题解 A. Problem Generator 题解&#xff1a;这题水题一个&#xff0c;我们来考虑本题的…

Dynamics 365:安全的客户参与应用程序

客户参与应用程序使用Microsoft Dataverse提供了一个丰富的安全模型&#xff0c;可以适应许多业务场景。本节为您提供了应考虑的安全措施的特定于产品的指导。 Dataverse安全模型有以下目标&#xff1a; 只允许用户访问他们工作所需的信息。按角色对用户进行分组&#xff0c;并…