解雇相机并返回应用程序需要很长时间

我有一个应用程序让用户添加他的图像 . 为此,我使用UIImagePicker .

请看一下以下功能:当点击图像时,它会打开一个视图作为弹出窗口 .

func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{


    let userDetails:Dictionary = (UserDefaults.standard.value(forKey: "myUserDetails") as? [String:Any])!
    let UserID:Int = userDetails["UserID"] as! Int

    let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "imager") as! imagerVC
    popOverVC.UserID = UserID

    self.addChildViewController(popOverVC)
    popOverVC.view.frame = self.view.frame
    self.view.addSubview(popOverVC.view)

    popOverVC.didMove(toParentViewController: self)


    popOverVC.callback = { image in
        // do something with the image


        self.profile_image.image = image
        if let data = UIImagePNGRepresentation(image) {
            //save profile image as NewUserID
            UserDefaults.standard.set(data, forKey: String(UserID))
            UserDefaults.standard.synchronize()
        }
    }  
}

这是激活相机的View代码,此视图从上面的代码中以popOverVC打开 .

import UIKit

    class imagerVC: UIViewController,  UIImagePickerControllerDelegate, UINavigationControllerDelegate {
        var UserID:Int = 0
        @IBOutlet weak var myImageView: UIImageView!
        var callback : ((UIImage) -> ())?

        @IBOutlet weak var btn_end: UIButton!
        @IBOutlet weak var from_camera: UIBarButtonItem!
        @IBOutlet weak var from_gallery: UIBarButtonItem!

        @IBAction func btn_end_pressed(_ sender: UIButton) {
             self.view.removeFromSuperview()
        }

        @IBAction func btn_closer(_ sender: UIButton) {
             self.view.removeFromSuperview()
        }


        @IBAction func photofromLibrary(_ sender: UIBarButtonItem) {
            picker.allowsEditing = false
            picker.sourceType = .photoLibrary
            picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
            picker.modalPresentationStyle = .popover
            present(picker, animated: true, completion: nil)
            picker.popoverPresentationController?.barButtonItem = sender
        }



        @IBAction func shootPhoto(_ sender: UIBarButtonItem) {
            if UIImagePickerController.isSourceTypeAvailable(.camera) {

                UserDefaults.standard.set(true, forKey: "is_selfie")
                UserDefaults.standard.synchronize()

                DispatchQueue.main.async {
                    self.picker.allowsEditing = false
                    self.picker.sourceType = UIImagePickerControllerSourceType.camera
                    self.picker.cameraCaptureMode = .photo
                    self.picker.modalPresentationStyle = .fullScreen
                    self.present(self.picker,animated: true,completion: nil)
                }

            } else {
                noCamera()
            }    
        }


        let picker = UIImagePickerController()

        override func viewDidLoad() {
            super.viewDidLoad()
            picker.delegate = self   
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }


        //MARK: - Delegates
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
        {
            let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
            myImageView.contentMode = .scaleAspectFit //3
            myImageView.image = chosenImage //4


            myImageView.layer.borderWidth = 1
            myImageView.layer.masksToBounds = false
            myImageView.layer.borderColor = UIColor.black.cgColor
            myImageView.layer.cornerRadius = myImageView.frame.height/4
            myImageView.clipsToBounds = true

            callback?(chosenImage)

            dismiss(animated:true, completion: nil)  
        }

        func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
            dismiss(animated: true, completion: nil)
        }

        func noCamera(){
            let alertVC = UIAlertController(
                title: "No Camera",
                message: "Sorry, this device has no camera",
                preferredStyle: .alert)
            let okAction = UIAlertAction(
                title: "OK",
                style:.default,
                handler: nil)
            alertVC.addAction(okAction)
            present(
                alertVC,
                animated: true,
                completion: nil)
        }       
}

我有两个问题 . 也许他们是相关的 . 主要问题是,在摄像机视图加载并拍摄照片后,当我按下按钮使用照片时,摄像机不会立即被解除,并且在摄像机视图被解除之前会有大约一分钟的延迟 . 顺便说一下,回调函数会被按下“使用照片”按钮 .

我不确定这是否已连接,但我得到以下警告:实例方法'imagePickerController(_:didFinishPickingMediaWithInfo :)'几乎匹配要求当我让Xcode自动更正我相机功能停止运行时 .

我尝试在DispatchQueue.main.async中包装行dismiss(animated:true,completion:nil)以使其在主线程上运行但是不起作用

不知道为什么但物理IPad中只出现滞后,只有iPhone设备

任何提供的帮助非常感谢

回答(1)

2 years ago

这是模拟器上的预期行为 . 它不应该落后于设备,因为你说它在设备上工作然后很酷 . !