swift相机启动

1.在info.plist中添加参数

Privacy - Camera Usage Description   允许使用相机

Privacy - Photo Library Usage Description  允许使用图库


2.代码  

注意:类要继承 

UIImagePickerControllerDelegate

/**

     判断相机权限

     

     - returns: 有权限返回true,没权限返回false

     */

    func cameraPermissions() -> Bool{

        

        let authStatus:AVAuthorizationStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)

        

        if(authStatus == AVAuthorizationStatus.denied || authStatus == AVAuthorizationStatus.restricted) {

            return false

        }else {

            return true

        }

        

    }

// TODO: 调用相机拍照

    func PaiZhao(){

        if(self.cameraPermissions() == true){

            let imagePicker = UIImagePickerController()

            //判断设备是否有相机

            if UIImagePickerController.isSourceTypeAvailable(.camera){

                // 把UIImagePicker-Controller的委托属性设置为DetailViewController对象

                imagePicker.delegate = self

                // 调用摄像机

                imagePicker.sourceType = .camera

                // 以模态的的形式显示。其视图会占据整个屏幕

                self.present(imagePicker, animated: true, completion: nil)

            }else{

                CommonFunction.TanChuang(msg: "无法调用相机,联系管理员", uiview: self)

                // 让用户选择相册,重相册中选取一张照片

                //            imagePicker.sourceType = .photoLibrary

            }

        }else{

            //弹出提示框

            self.sheet = UIAlertController(title: nil, message: "请在设置中打开摄像头权限", preferredStyle: .alert)

            

            let tempAction = UIAlertAction(title: "确定", style: .cancel) { (action) in

            }

            self.sheet.addAction(tempAction)

            self.present(self.sheet, animated: true, completion: nil)

        }

    }

// TODO: 摄像机委托方法

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        // 重数据源中获取图片

        let image = info[UIImagePickerControllerOriginalImage] as! UIImage

        

        // 将图片保存内存当中

//        imageStore.setImage(image: image, forKey: item.itemKey)

        // 设置显示图片

        imageView.image = image

        

        // 关闭摄像机模态窗

        // 必须调用这种方法关闭

        dismiss(animated: true, completion: nil)

    }


SwiftUI 中,可以使用 `@main` 属性和 `@UIApplicationDelegateAdaptor` 属性来创建和设置应用程序的启动页面。 首先,你需要创建一个遵循 `App` 协议的主应用程序结构体。在这个结构体中,使用 `@UIApplicationDelegateAdaptor` 属性来指定一个自定义的 `AppDelegate` 类。 ```swift @main struct YourAppName: App { @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate var body: some Scene { WindowGroup { ContentView() } } } ``` 然后,创建一个自定义的 `AppDelegate` 类,继承自 `UIResponder` 并实现 `UIApplicationDelegate` 协议。在 `AppDelegate` 类中,你可以设置启动页面的样式和持续时间。 ```swift import SwiftUI class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { // 设置启动页面的样式 application.isStatusBarHidden = true // 设置启动页面的持续时间 Thread.sleep(forTimeInterval: 3.0) return true } } ``` 在上述代码中,我们将应用程序的状态栏隐藏,并设置启动页面的持续时间为 3 秒钟。你可以根据自己的需求进行定制。 最后,在 `WindowGroup` 中设置你想要显示的内容,这里的 `ContentView` 可以是你的应用程序的主视图。 这样就完成了在 SwiftUI 中创建和设置启动页面的过程。记得在 `info.plist` 文件中设置你的应用程序的主入口文件为 `YourAppName`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值