首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >选择“照相机”会因信号9而终止

选择“照相机”会因信号9而终止
EN

Stack Overflow用户
提问于 2017-04-23 05:24:34
回答 3查看 1.9K关注 0票数 3

在iOS SWIFT3.1中,我试图访问相机,就像我在其他应用程序中成功地做的那样。然而,在这个特定的应用程序中,它总是在self.present(imagePicker, animated: true, completion: nil)行上崩溃。控制台中的消息是Message from debugger: Terminated due to signal 9。这通常表示与内存相关的错误吗?

代码语言:javascript
复制
@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)
    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-04-25 01:10:40

您需要在您的plist文件中的字符串,解释为什么您的应用程序需要许可使用相机。在iOS 10 - Changes in asking permissions of Camera, microphone and Photo Library causing application to crash的问题中有一个关于这些字符串的很好的总结,您可以在https://stackoverflow.com/a/40734360/968577的答案中复制一个列表

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

票数 3
EN

Stack Overflow用户

发布于 2017-04-23 06:40:18

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

  1. 私隐相机使用说明
  2. 隐私-照片库使用说明

将这两个委托添加到类文件中。

  1. UIImagePickerControllerDelegate
  2. UINavigationControllerDelegate @IBAction (_onclickImageAction: Any){ print("onclickImageAction方法在这里调用“)让imagePicker = UIImagePickerController() imagePicker.delegate = self imagePicker.isEditing = false let =UIAlertController(标题:”selected“,message:”,preferredStyle: UIAlertControllerStyle.actionSheet)让libButton =UIAlertAction(标题:“从库中选择照片”,样式: UIAlertActionStyle.default){ (libSelected) ){(LibSelected)在打印(“库被选中”) imagePicker.sourceType =onclickImageAction self.present(libSelected,动画: true :true),完成:0)} actionSheet.addAction(libButton)让cameraButton =UIAlertAction(标题:“拍照”,样式: UIAlertActionStyle.default) { (camSelected)在if { imagePicker.sourceType = UIImagePickerControllerSourceType.camera self.present(imagePicker,动画:真,完成:0)}cameraButton{actionSheet.dismiss(动画:假,完成:零)让警报=UIAlertController(标题:“错误”,消息:“没有可用的照相机”,preferredStyle:.alert) alert.addAction(标题:“is”,样式:.default,处理程序:{ (alertAction) in alert.dismiss(动画:真,完成:零)}(CameraButton)让cancelButton =UIAlertAction(标题:“取消”,样式: UIAlertActionStyle.cancel) { (cancelSelected)在打印(“cancel selected") } actionSheet.addAction(cancelButton)让albumButton =UIAlertAction(标题:”已保存的相册“,style: UIAlertActionStyle.default) { (albumSelected) {(AlbumSelected)在打印(”相册选定“) imagePicker.sourceType =actionSheet.addAction self.present(imagePicker,动画:真,完成:0)} actionSheet.addAction(albumButton) self.present(actionSheet,动画:真,完成:0)}self.present

实现这些委托方法

代码语言:javascript
复制
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{if let PickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
        {
            yourimageview.image = PickedImage
            dismiss(animated: true, completion: nil)   
        }
    }
票数 0
EN

Stack Overflow用户

发布于 2017-10-12 10:39:42

打开相机设置时,我刚刚添加了exit(0)。很好用

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43567726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档