Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 选择";照相机“;因信号9而终止的原因_Ios_Swift_Memory_Crash_Uiimagepickercontroller - Fatal编程技术网

Ios 选择";照相机“;因信号9而终止的原因

Ios 选择";照相机“;因信号9而终止的原因,ios,swift,memory,crash,uiimagepickercontroller,Ios,Swift,Memory,Crash,Uiimagepickercontroller,在iOS Swift 3.1中,我尝试访问摄像头,这与我在其他应用程序中成功访问的方式相同。然而,在这个特定的应用程序中,它总是在self.present(imagePicker,动画:true,完成:nil)行上崩溃。控制台中的消息是来自调试器的消息:由于信号9而终止。这通常是内存相关错误的信号吗 @IBAction func onChooseImageClick(_ sender: AnyObject) { if UIImagePickerController.isSourceTyp

在iOS Swift 3.1中,我尝试访问摄像头,这与我在其他应用程序中成功访问的方式相同。然而,在这个特定的应用程序中,它总是在
self.present(imagePicker,动画:true,完成:nil)行上崩溃。控制台中的消息是来自调试器的
消息:由于信号9而终止。这通常是内存相关错误的信号吗

@IBAction func onChooseImageClick(_ sender: AnyObject) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.savedPhotosAlbum){

        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self

        //Create the AlertController and add Its action like button in Actionsheet
        let actionSheetControllerForImage: UIAlertController = UIAlertController(title: "Please select", message: "Option to select", preferredStyle: .actionSheet)

        let cancelActionButton: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in
            print("Cancel")
        }
        actionSheetControllerForImage.addAction(cancelActionButton)

        let cameraActionButton: UIAlertAction = UIAlertAction(title: "Camera", style: .default)
        { action -> Void in
            print("Camera")
            if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)) {
                imagePicker.sourceType = UIImagePickerControllerSourceType.camera
                let mediaTypes:[String] = [kUTTypeImage as String]
                imagePicker.mediaTypes = mediaTypes
                imagePicker.allowsEditing = false

                self.present(imagePicker, animated: true, completion: nil)
            } else {
                let alertController = UIAlertController(title: "error", message: "Camera not found!", preferredStyle: .alert)

                let OKAction = UIAlertAction(title: "OK", style: .cancel) { action in
                    // ...
                }
                alertController.addAction(OKAction)

                self.present(alertController, animated: true)
            }
        }
        actionSheetControllerForImage.addAction(cameraActionButton)

        let galleryActionButton: UIAlertAction = UIAlertAction(title: "Image Gallery", style: .default)
        { action -> Void in
            imagePicker.sourceType = UIImagePickerControllerSourceType.savedPhotosAlbum;
            imagePicker.allowsEditing = false
            self.present(imagePicker, animated: true, completion: nil)
        }
        actionSheetControllerForImage.popoverPresentationController?.sourceView = self.view
        actionSheetControllerForImage.addAction(galleryActionButton)
   ===> self.present(actionSheetControllerForImage, animated: true, completion: nil)
    }
}

像这样试试,我希望这会有帮助!! 首先在info.plist中添加这两点

  • 隐私-相机使用说明
  • 隐私-照片库使用说明
  • 将这两个委托添加到类文件中

  • UIImagePickerControllerDelegate
  • UINavigationControllerDelegate

     @IBAction func onclickImageAction(_ sender: Any){
    
    print("onclickImageAction method called here")
    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.isEditing = false
    
    let actionSheet =  UIAlertController(title: "Select Profile Photo", message: "", preferredStyle: UIAlertControllerStyle.actionSheet)
    
    let libButton = UIAlertAction(title: "Select photo from library", style: UIAlertActionStyle.default){ (libSelected) in
        print("library selected")
    
    
        imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
        self.present(imagePicker, animated: true, completion: nil)
    }
    actionSheet.addAction(libButton)
    let cameraButton = UIAlertAction(title: "Take a picture", style: UIAlertActionStyle.default) { (camSelected) in
    
        if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera))
        {
            imagePicker.sourceType = UIImagePickerControllerSourceType.camera
            self.present(imagePicker, animated: true, completion: nil)
        }
        else
        {
            actionSheet.dismiss(animated: false, completion: nil)
            let alert = UIAlertController(title: "Error", message: "There is no camera available", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Okay", style: .default, handler: { (alertAction) in
    
                alert.dismiss(animated: true, completion: nil)
            }))
    
        }
    
    }
    actionSheet.addAction(cameraButton)
    let cancelButton = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) { (cancelSelected) in
    
        print("cancel selected")
    
    }
    actionSheet.addAction(cancelButton)
    let albumButton = UIAlertAction(title: "Saved Album", style: UIAlertActionStyle.default) { (albumSelected) in
    
        print("Album selected")
    
        imagePicker.sourceType = UIImagePickerControllerSourceType.savedPhotosAlbum
        self.present(imagePicker, animated: true, completion: nil)
    
    }
    actionSheet.addAction(albumButton)
    self.present(actionSheet, animated: true, completion:nil)
    }
    
  • 实现这些委托方法

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
    {if let PickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
            {
                yourimageview.image = PickedImage
                dismiss(animated: true, completion: nil)   
            }
        }
    

    你的plist文件中需要一个字符串来解释为什么你的应用程序需要使用相机的权限。在的问题中有这些字符串的一个很好的摘要,在的答案中有一个列表可以复制


    如果没有必需的字符串,您的应用程序将以这种方式崩溃。但是,控制台输出将解释问题所在。

    我刚在打开相机设置时添加了退出(0)。它工作正常

    你的plist文件中需要一个字符串来解释为什么你的应用程序需要使用摄像头的权限。有关这些字符串的详细信息,请参阅。@GaryMakin我以为我的plist中有这些字符串,但我想我是在看“库”字符串。这解决了问题。非常感谢@GaryMakin,是否要包括您的解决方案作为答案?我刚在打开相机设置时添加了退出(0)。很好用