SwiftUI——如何调用相机

SwiftUI调用相机和调用相册其实是一个思路,只用修改一个地方。相册调用请看这里。

我们继续来说相机调用,SwiftUI没办法自己获取相机,所以得依靠一下UIKit,所以头文件如下:

import SwiftUI
import UIKit

然后和调用相册一样,我们创建一个ImagePicker结构体供我们获取系统相册内容(所以如果我们需要在一个软件里调用相册和相机的话,就只用创建一个ImagePacker结构体即可):

struct ImagePicker: UIViewControllerRepresentable {
    @Environment(\.presentationMode) private var presentationMode
    let sourceType: UIImagePickerController.SourceType
    let onImagePicked: (UIImage) -> Void
 
    final class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
 
        @Binding private var presentationMode: PresentationMode
        private let sourceType: UIImagePickerController.SourceType
        private let onImagePicked: (UIImage) -> Void
 
        init(presentationMode: Binding<PresentationMode>,
             sourceType: UIImagePickerController.SourceType,
             onImagePicked: @escaping (UIImage) -> Void) {
            _presentationMode = presentationMode
            self.sourceType = sourceType
            self.onImagePicked = onImagePicked
        }
 
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            let uiImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
            onImagePicked(uiImage)
            presentationMode.dismiss()
        }
 
        func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
            presentationMode.dismiss()
        }
 
    }
 
    func makeCoordinator() -> Coordinator {
        return Coordinator(presentationMode: presentationMode,
                           sourceType: sourceType,
                           onImagePicked: onImagePicked)
    }
 
    func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
        let picker = UIImagePickerController()
        picker.sourceType = sourceType
        picker.delegate = context.coordinator
        return picker
    }
 
    func updateUIViewController(_ uiViewController: UIImagePickerController,
                                context: UIViewControllerRepresentableContext<ImagePicker>) {
    }
}

不一样的地方在调用的时候,在相册调用的时候,sourceType部分我们使用的是.photoLibrary,这里我们只要将其改成.camera就可以啦,如下:

struct ContentView: View {
    @State private var showCameraPicker = false
    //这里的image用于放置等会拍摄了的照片
    @State private var image: UIImage = UIImage()
    var body: some View {
        List{
            Button(action: {
                showCameraPicker = true
            }, label: {
                Text("Camera")
            })
            
            Image(uiImage: image)
                .resizable()
                .aspectRatio(contentMode: .fit)
        }
        .sheet(isPresented: $showCameraPicker,
               content: {
            ImagePicker(sourceType: .camera) { image in
                self.image = image
            }
        })
    }
}

这样我们就能调用系统相机了。

你可能感兴趣的:(Swift/SwiftUI,swiftui,ios)