提问者:小点点

SwiftiOS-UIImagePicker的照片库显示在模拟器上,但在运行Xcode时在实际设备上崩溃(不会出现)


我用的是Swift 3,iOS 10.3,Xcode 8.3.3。

当我在模拟器上访问照片库时,UIImagePicker没有问题,我可以选择照片。一旦我尝试访问实际设备(iPhone 7)上的照片库,应用程序就会崩溃,因为UIImagePicker不会显示(我什至没有到选择照片的地步)。奇怪的是,在实际设备上,imagePicker呈现相机,我可以毫无问题地拍照。

尽管我没有把它添加到这个问题中,但是在我的实际代码中,我有api权限在运行。在我的实际代码中,我使用了它们并注释掉了它们,但问题仍然出现:

import AVFoundation

//there is way more code then this I just put these here to show that I'm using them
PHPhotoLibrary.authorizationStatus()
PHPhotoLibrary.requestAuthorization()
AVCaptureDevice.authorizationStatus(forMediaType: ...)
AVCaptureDevice.requestAccess(forMediaType: ...)

更新:我仍然没有解决这个问题,但我发现了一些更多的信息。我一直在玩这个应用程序,在一次崩溃后我再次尝试了这个应用程序,它让我可以访问图书馆。我开始意识到,只要Xcode没有运行,这个应用程序就可以让我在手机上访问这个库。当我把手机插回Xcode并试图再次访问库时,我遇到了崩溃。我不知道那是怎么回事。

只要Xcode没有连接到设备,我就可以成功地从库中获取照片。使用模拟器,我可以毫无问题地获取照片。如果设备没有连接到Xcode,我就可以获取照片。然而,一旦设备和Xcode连接,我就会崩溃。我尝试了3种不同的方式初始化UIImagePicker(如下所述),看看它们中的任何一种是否会改变结果,但结果总是一样的。我的应用程序超过100个类和50个vcs,也许问题发生在其他地方。然而,如果它发生在其他地方,那么当Xcode也没有连接到设备时,它应该会崩溃。

在使用UIImagepicker的类中,我注释掉了所有不必要的代码,只关注库和相机按钮。一旦设备和Xcode交织在一起,崩溃总是会发生。好消息是,一旦它们没有连接,我就可以访问库,这样用户就不会有问题,因为他们永远不会让Xcode与我的应用程序一起运行。

应该注意的是,当设备和Xcode连接时,我可以使用相机并成功拍照。这个问题似乎由图书馆监管。

我看到这个问题被问了几次,但答案总是

“必须设置信息列表内的权限”

在我的情况下,权限是在 info.plist 中设置的:

这是模拟器设置页面中设置为“开”的开关的照片。我无法在实际设备的设置页面上拍照,但在那里相机和照片开关都设置为“开”

我的代码:

MyController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{ 

fileprivate var imagePicker = UIImagePickerController()

override func viewDidLoad() {
        super.viewDidLoad()

        imagePicker.delegate = self
}

@IBAction fileprivate func libraryButton(_ sender: UIButton){

        self.imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
        self.imagePicker.allowsEditing = false
        presentViewController(self.imagePicker, animated: true, completion: nil)
 }

@IBAction fileprivate func cameraButton(_ sender: UIButton){

        self.imagePicker.sourceType = UIImagePickerControllerSourceType.camera
        self.imagePicker.allowsEditing = false
        present(self.imagePicker, animated: true, completion: nil)
 }

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

        if let myImage = info[UIImagePickerControllerOriginalImage] as? UIImage{
            imageView.image = myImage
        }
        else if let myImage = info[UIImagePickerControllerEditedImage] as? UIImage{
            imageView.image = myImage
        }
        else{"UIImagePicker Problem")
        }

        imagePicker.dismiss(animated: true, completion: nil)
    }

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

我也试过:

fileprivate var imagePicker:UIImagePickerController?

@IBAction fileprivate func libraryButton(_ sender: UIButton){

        self.imagePicker = UIImagePickerController()
        self.imagePicker?.delegate = self
        self.imagePicker?.sourceType = UIImagePickerControllerSourceType.photoLibrary
        self.imagePicker?.allowsEditing = false
        present(self.imagePicker!, animated: true, completion: nil)
 }

@IBAction fileprivate func cameraButton(_ sender: UIButton){

        self.imagePicker = UIImagePickerController()
        self.imagePicker?.delegate = self
        self.imagePicker?.sourceType = UIImagePickerControllerSourceType.camera
        self.imagePicker?.allowsEditing = false
        present(self.imagePicker!, animated: true, completion: nil)
 }

我也试过:

fileprivate let imagePicker:UIImagePickerController?

override func viewDidLoad() {
        super.viewDidLoad()

        imagePicker = UIImagePickerController()
        imagePicker!.delegate = self
}


@IBAction fileprivate func libraryButton(_ sender: UIButton){

        imagePicker!.sourceType = UIImagePickerControllerSourceType.photoLibrary
        imagePicker!.allowsEditing = false
        present(imagePicker!, animated: true, completion: nil)
 }

@IBAction fileprivate func cameraButton(_ sender: UIButton){

        imagePicker!.sourceType = UIImagePickerControllerSourceType.camera
        imagePicker!.allowsEditing = false
        present(imagePicker!, animated: true, completion: nil)
 }

这是我在设备和Xcode连接后一直得到的相同的崩溃信息:

libc++abi.dylib`__cxa_throw:
->  0x18fd96e70 <+0>:   stp    x22, x21, [sp, #-0x30]!
    0x18fd96e74 <+4>:   stp    x20, x19, [sp, #0x10]
    0x18fd96e78 <+8>:   stp    x29, x30, [sp, #0x20]
    0x18fd96e7c <+12>:  add    x29, sp, #0x20            ; =0x20 
    0x18fd96e80 <+16>:  mov    x19, x2
    0x18fd96e84 <+20>:  mov    x20, x1
    0x18fd96e88 <+24>:  mov    x21, x0
    0x18fd96e8c <+28>:  bl     0x18fd96a88               ; __cxa_get_globals
    0x18fd96e90 <+32>:  mov    x22, x0
    0x18fd96e94 <+36>:  bl     0x18fd9756c               ; std::get_unexpected()
    0x18fd96e98 <+40>:  stur   x0, [x21, #-0x60]
    0x18fd96e9c <+44>:  bl     0x18fd975ac               ; std::get_terminate()
    0x18fd96ea0 <+48>:  stur   x0, [x21, #-0x58]
    0x18fd96ea4 <+52>:  stp    x20, x19, [x21, #-0x70]
    0x18fd96ea8 <+56>:  mov    x8, #0x434c000000000000
    0x18fd96eac <+60>:  movk   x8, #0x4e47, lsl #32
    0x18fd96eb0 <+64>:  movk   x8, #0x432b, lsl #16
    0x18fd96eb4 <+68>:  movk   x8, #0x2b00
    0x18fd96eb8 <+72>:  mov    x19, x21
    0x18fd96ebc <+76>:  str    x8, [x19, #-0x20]!
    0x18fd96ec0 <+80>:  orr    w8, wzr, #0x1
    0x18fd96ec4 <+84>:  stur   x8, [x19, #-0x58]
    0x18fd96ec8 <+88>:  ldr    w8, [x22, #0x8]
    0x18fd96ecc <+92>:  add    w8, w8, #0x1              ; =0x1 
    0x18fd96ed0 <+96>:  str    w8, [x22, #0x8]
    0x18fd96ed4 <+100>: adrp   x8, 0
    0x18fd96ed8 <+104>: add    x8, x8, #0xef8            ; =0xef8 
    0x18fd96edc <+108>: str    x8, [x19, #0x8]
    0x18fd96ee0 <+112>: mov    x0, x19
    0x18fd96ee4 <+116>: bl     0x190433be4               ; _Unwind_RaiseException
    0x18fd96ee8 <+120>: mov    x0, x19
    0x18fd96eec <+124>: bl     0x18fd96f20               ; __cxa_begin_catch
    0x18fd96ef0 <+128>: ldur   x0, [x21, #-0x58]
    0x18fd96ef4 <+132>: bl     0x18fd975c4               ; std::__terminate(void (*)())

我的项目中没有放松Segue,所以我不知道为什么它会这么说。但是不管它是否在模拟器上工作,它都应该在手机上工作,或者如果它在手机上不工作,那么它也不应该在模拟器上工作。下面是崩溃报告和堆栈跟踪的片段。

错误报告:

我从设备中删除了该应用程序,关闭了Xcode,重新启动了它,接受了ok以让它访问库,以及同样的问题。

知道问题是什么吗?


共3个答案

匿名用户

Ash Furrow帮助我解决了这个问题,因为这是一个非常不明显的问题,可能是一个api错误。我为不熟悉解决问题的过程的人发布了照片。

原来在< code >断点导航器中,我激活了< code >所有异常。由于某种原因,当我的应用程序连接到Xcode并且UIImagePicker正在显示照片库时,所有异常断点被触发,并且有一点延迟,导致应用程序崩溃。延迟是毫秒级的,但它足够长,以至于应用程序认为有问题。Ash说他以前从未见过这样的东西,并说在我的照片库中可能有一张损坏的jpg照片,但这又是一个胡乱的猜测。简单的解决方案是停用或删除所有异常断点,问题就解决了。他说这可能是api的问题,因为它仍然导致了一个非常奇怪的崩溃。

应该注意的是,他说,“所有异常”断点保持激活状态是很好的,因为它会在更早的时候发现异常。我忘记了他的详细解释,但基本上,最好保持它,如果你遇到这个问题,就停用它。

通过停用所有异常断点来解决UIImagePicker库表示崩溃的步骤:

  1. 在导航窗格的屏幕左侧,左侧第7个看起来像箭头的图标是断点导航器

要停用所有例外,请单击突出显示的蓝色箭头或右键单击单词“所有例外”本身,然后从列表中选择禁用或删除

匿名用户

尝试使用以下代码非常适合我

  @IBAction func openLibraryButtonAction(_ sender: Any) {


        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary;
            imagePicker.allowsEditing = true
            self.present(imagePicker, animated: true, completion: nil)
        }

    }


 @IBAction func takePhotoButtonAction(_ sender: Any)
{

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)
    {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
        imagePicker.allowsEditing = false
        self.present(imagePicker, animated: true, completion: nil)
    }
    else
    {
        noCamera() //Calling alert of No Camera
    }
}


func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
    {
        chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
dismiss(animated:true, completion: nil)
}

匿名用户

我将证明一些建议的合理性,然后粘贴结果片段。

>

  • 确保从UIViewController继承,以便使用viewDidLoad()等方法
  • imagePicker设置为init,因此如果让链接,请删除所有问号或
  • 为了保持一致,只需删除任何“自我”不需要的
  • 将任何冗余设置移动到viewDidLoad()

    class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    fileprivate let imagePicker = UIImagePickerController()
    
    override func viewDidLoad() {
    super.viewDidLoad()
    
      imagePicker.delegate = self
      imagePicker.allowsEditing = false
    }
    
    @IBAction fileprivate func libraryButton(_ sender: UIButton) {
    
      imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
      present(imagePicker, animated: true, completion: nil)
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    
      if let myImage = info[UIImagePickerControllerOriginalImage] as? UIImage{
        //original image
      }
      else if let myImage = info[UIImagePickerControllerEditedImage] as? UIImage{
        //edited image
      }
      else{
        print("image picker problem")
      }
    
      imagePicker.dismiss(animated: true, completion: nil)
    }
    
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    
      imagePicker.dismiss(animated: true, completion: nil)
    }
    
      }
    

    有了这些变化,你应该会很好。