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

Swift iOS -UIImagePicker's Photo Library Is Presented On Simulator But Crashes (won't present) On Actual Device While Running 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 不是 运行,该应用程序就会让我访问 phone 上的图书馆。一旦我将 phone 重新插入 Xcode 并尝试再次访问该库,我就会崩溃。我不知道该怎么做。

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

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

需要注意的是,当设备和Xcode连接后,我可以使用相机并成功拍照。问题好像是调节到图书馆了。

这个问题我已经看过好几次了,但答案总是

"the permissions inside your info.plist have to be set"

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

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

我的代码:

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 (*)())

我的项目中的任何地方都没有 Unwind Segue,所以我不确定为什么会这样说。但无论它是否在模拟器上工作,它都应该在 phone 上工作,或者如果它在 phone 上不起作用,那么它也不应该在模拟器上工作。下面是崩溃报告和堆栈跟踪的片段。

错误报告:

我从设备上删除了该应用程序,关闭 Xcode,重新启动它,接受并允许它访问库,但还是出现了同样的问题。

知道问题出在哪里吗?

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

  @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)
}

你代码的问题是UIImagePickerControllerSourceType.PhotoLibrary,图片库改成.photoLibrary p应该是小写

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

  • 确保从 UIViewController 继承,以便使用像 viewDidLoad() 这样的方法
  • imagePicker 在初始化时设置,因此删除所有问号或 if let 链接。
  • 为了保持一致性,只需删除不需要的'self.'
  • 将任何冗余设置移动到 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)
    }
    
      }
    

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

您必须明确请求访问使用 UIImagePicker 的图片库。

比如PHPhotoLibrary.requestAuthorization

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

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

需要注意的是,他说All Exceptions break point最好保持激活,因为它会早发现异常。我忘记了他的详细解释,但基本上最好继续使用,如果您 运行 遇到这个问题,只需停用它即可。

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

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

  1. All Exceptions 断点是activated/highlighted到蓝色

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

  1. 假设您选择禁用或单击蓝色箭头,它将被停用

  1. 显然,如果您选择删除,它将从断点导航器中删除,但如果您想再次添加它(或者如果您从来没有使用过它),那么您仍然必须在断点导航器中(第 1 步) ) 并在窗格的左下角按加号按钮并选择异常断点以添加它。

**- I'm also facing the same problem after selecting any image from gallery. after lots of effects I found this solution.**

if #available(iOS 14, *) {
            var config = PHPickerConfiguration()
            config.filter = .images
            config.selectionLimit = 1
            let picker = PHPickerViewController(configuration: config)
            picker.delegate = self
            self.currentController.present(picker, animated: true)
        } else {
            if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary) {
                self.imagePicker.delegate = self
                self.imagePicker.allowsEditing = false
                self.imagePicker.sourceType = .savedPhotosAlbum
                self.currentController.present(imagePicker, animated: true, completion: nil)
            } else {
                let alert  = UIAlertController(title: "Warning", message: "You don't have permission to access gallery.", preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                self.currentController.present(alert, animated: true, completion: nil)
            }
        }


extension USE_YOUR_CLASS_NAME: PHPickerViewControllerDelegate {
    @available(iOS 14, *)
    func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
        guard let result = results.first else { return }

        let jpeg = UTType.jpeg.identifier
        let prov = result.itemProvider
        prov.loadFileRepresentation(forTypeIdentifier: jpeg) { urlMaybe, errorMaybe in
            guard let url = urlMaybe else { return }

            let imageData:NSData = NSData(contentsOf: urlMaybe!)!
            let image = UIImage(data: imageData as Data)
            if image != nil {
                // YOU'LL GET SELECTED IMAGE HERE..
            }
        }
    }
}


extension USE_YOUR_CLASS_NAME: UINavigationControllerDelegate, UIImagePickerControllerDelegate {
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    
    //guard let fileUrl = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else { return }
    debugPrint(selectedImgName)
    
    if let chosenImage = info[.originalImage] as? UIImage {
        // YOU'LL GET SELECTED IMAGE HERE..
    }
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    currentController.dismiss(animated: true, completion: nil)
}
}