我在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 ISN&T连接到设备,我就可以成功获取图书馆中的照片。使用模拟器,我可以轻松获取照片。由于设备未连接到Xcode,我可以获取照片。然而,一旦设备和Xcode连接,我就会崩溃。我尝试了以不同的方式初始化UIImagePicker 3(如下所述),看看它们中的任何一个是否会改变结果,但它总是相同的。我的应用程序超过100个类和50个vcs,也许问题发生在其他地方。但是,如果它发生在其他地方,那么当Xcode ISN连接到设备时它会崩溃。
在使用UIImagepicker的类中,我注释掉了所有不必要的代码,并只关注库和相机按钮。一旦设备和Xcode交织在一起,就会发生崩溃。好消息是,一旦他们没有连接,我就可以访问该库,因此用户不会遇到问题,因为他们从未让我的应用程序运行Xcode。
应该注意的是,当设备和Xcode连接起来时,我可以使用相机并成功拍摄照片。这个问题似乎受到了图书馆的限制。
我已经多次问过这个问题,但答案总是
"必须设置info.plist中的权限"
在我的情况下,权限是在info.plist中设置的:
这是Switch的图片,设置为" on"在模拟器的“设置”页面中。我无法拍摄实际设备的设置页面,但在那里相机和照片开关都设置为" 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 (*)())
我的项目中的任何地方都没有“放松风暴”,所以我不确定为什么会这么说。但无论它是否在模拟器上工作,它都可以在手机上工作,或者如果它不能在手机上工作,那么它也不应该在模拟器上工作。下面是崩溃报告和堆栈跟踪的片段。
错误报告:
我从设备中删除了应用程序,关闭了Xcode,重新启动了它,接受了确定可以访问该库,同样的问题。
知道问题是什么?
答案 0 :(得分:2)
事实证明,Breakpoint Navigator
已激活All Exceptions
。出于某种原因,当我的应用程序连接到Xcode并且UIImagePicker呈现照片库时,触发了All Exceptions断点,并且稍有延迟导致应用程序崩溃。延迟是毫秒,但足够长,应用程序认为存在问题。 Ash说他以前从未见过这样的东西,并且说我的照片库里可能有一张损坏的jpg照片,但是又一次疯狂猜测。简单的解决方案是停用或删除All Exceptions断点,问题已解决。他确实说这可能是一个api问题,因为它仍然造成了非常奇怪的崩溃。
应该注意的是,他说所有异常断点都可以保持激活,因为它会在之前发现异常。我详细地忘记了他的解释,但基本上最好保持它,如果遇到这个问题,只需停用它。
通过停用所有异常断点来解决UIImagePicker库演示文稿崩溃的步骤:
答案 1 :(得分:0)
尝试使用以下代码可以完美地为我工作
@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)
}
答案 2 :(得分:0)
我将证明一些建议,然后粘贴生成的代码段。
if let
链接。将任何冗余设置移至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)
}
}
通过这些改变你应该很好。
答案 3 :(得分:0)
您必须明确请求访问使用UIImagePicker
。
例如PHPhotoLibrary.requestAuthorization
答案 4 :(得分:-1)
您的代码中的问题是UIImagePickerControllerSourceType.PhotoLibrary
,照片库更改为.photoLibrary
p 应为小写字母