iOS Swift 3 UIImagePickerController 崩溃

Posted

技术标签:

【中文标题】iOS Swift 3 UIImagePickerController 崩溃【英文标题】:iOS Swift 3 UIImagePickerController crash 【发布时间】:2016-10-12 09:47:45 【问题描述】:

我将项目升级到 Swift 3,但在线上发生了崩溃(在项目中的各个点/viewControllers):

present(picker, animated: true, completion: nil)

在函数中:

@IBAction func userImage(_ sender: AnyObject)
    print("button pressed")
    let picker = UIImagePickerController()
    picker.delegate = self
    picker.allowsEditing = false
    picker.sourceType = .photoLibrary
    present(picker, animated: true, completion: nil) // -> crashes here


func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) 
    let image = info[UIImagePickerControllerOriginalImage] as! UIImage
    self.imageToPost.setImage(newImage, for: UIControlState())
    self.imageToPost.imageView?.contentMode = UIViewContentMode.scaleAspectFill
    picker.dismiss(animated: true, completion: nil)

控制台中的崩溃消息:

libsystem_kernel.dylib`__abort_with_payload:
0x11108d138 <+0>:  movl   $0x2000209, %eax          ; imm = 0x2000209 
0x11108d13d <+5>:  movq   %rcx, %r10
0x11108d140 <+8>:  syscall 
->  0x11108d142 <+10>: jae    0x11108d14c               ; <+20>
0x11108d144 <+12>: movq   %rax, %rdi
0x11108d147 <+15>: jmp    0x111086d6f               ; cerror_nocancel
0x11108d14c <+20>: retq   
0x11108d14d <+21>: nop    
0x11108d14e <+22>: nop    
0x11108d14f <+23>: nop    

在我的 info.plist 中我有:

<key>NSPhotoLibraryUsageDescription</key>
<string>$(PRODUCT_NAME) photo gallery use</string>
<key>NSCameraUsageDescription</key>
<string>$(PRODUCT_NAME) camera use </string>
<key>NSMicrophoneUsageDescription</key>
<string>$(PRODUCT_NAME) microphone use</string>

我环顾四周,确实发现在 info.plist 中有 NSPhotoLibraryUsageDescription 和 NSCameraUsageDescription 可以解决问题(UIImagePickerController in Swift 3 和 UIImagePickerController crashes app | Swift3, Xcode8),但它不适合我。我做了清理和构建,重新启动了 xCode,重新启动了 Mac,等等。

值得指出的是,我的控制台在启动时显示:

objc[11204]: Class PLBuildVersion is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/AssetsLibraryServices.framework/AssetsLibraryServices (0x11a0d8910) and /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/PhotoLibraryServices.framework/PhotoLibraryServices (0x119f02210). One of the two will be used. Which one is undefined. 

但根据这个无关紧要的线程https://forums.developer.apple.com/thread/63254

我的班级有代表:

class PersonalSettingsVC: UIViewController, UITextFieldDelegate, UITextViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate 

非常感谢任何帮助

【问题讨论】:

设置两个委托 => UIImagePickerControllerDelegate, UINavigationControllerDelegate 您好,两位代表在场。感谢您的回答。 @PeterdeVries 您的应用程序从哪个点崩溃... 您需要授予相机访问权限。 @seggy 应用程序在“按下按钮”后崩溃。我也试过: present(picker, animated: true) print("succes") ,它没有出现在控制台中。我在 info.plist 中提供了相机访问权限,我是否需要在 VC 中也这样做? 【参考方案1】:

好的,所以我想通了。 (我生命中的 3 天)即使我将隐私密钥放在我的 info.plist 中,应用程序实际上并没有访问/识别它们。所以我必须去***应用程序->目标->“MY_APP”->信息并将它们也放在那里。之后,它跑了。 (Jecky、Seggy 和 KAR 的解决方案很棒,所以我投了赞成票)。显然是一个错误,希望它可以帮助某人。

【讨论】:

【参考方案2】:

对我来说,在 info.plist 中添加以下内容后它起作用了 隐私 - 相机使用说明 $(PRODUCT_NAME) 相机使用

【讨论】:

【参考方案3】:

试试我的代码 它是用相机拍照或从库中选择。只需给 UIImagePickerControllerDelegate,UINavigationControllerDelegate

在 Info.plist 文件中设置属性 隐私 - 照片库使用说明:允许照片

@IBAction func dsfsd(_ sender: AnyObject) 

        let actionSheetController: UIAlertController = UIAlertController(title: "Action Sheet", message: "Swiftly Now! Choose an option!", preferredStyle: .actionSheet)

        //Create and add the Cancel action
        let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel)  action -> Void in
            //Just dismiss the action sheet
        
        actionSheetController.addAction(cancelAction)
        //Create and add first option action
        let takePictureAction: UIAlertAction = UIAlertAction(title: "Take Picture", style: .default)  action -> Void in
            if(  UIImagePickerController.isSourceTypeAvailable(.camera))

            
                let myPickerController = UIImagePickerController()
                myPickerController.delegate = self
                myPickerController.sourceType = .camera
                self.present(myPickerController, animated: true, completion: nil)
            
            else
            
                let actionController: UIAlertController = UIAlertController(title: "Camera is not     available",message: "", preferredStyle: .alert)
                let cancelAction: UIAlertAction = UIAlertAction(title: "OK", style: .cancel)  action -> Void     in
                    //Just dismiss the action sheet
                

                actionController.addAction(cancelAction)
                self.present(actionController, animated: true, completion: nil)

            
        

        actionSheetController.addAction(takePictureAction)
        //Create and add a second option action
        let choosePictureAction: UIAlertAction = UIAlertAction(title: "Choose From Camera Roll", style: .default)  action -> Void in
            let myPickerController = UIImagePickerController()
            myPickerController.delegate = self;
            myPickerController.sourceType = UIImagePickerControllerSourceType.photoLibrary
            self.present(myPickerController, animated: true, completion: nil)
        
        actionSheetController.addAction(choosePictureAction)

        //Present the AlertController
        self.present(actionSheetController, animated: true, completion: nil)

    


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


        let image = info[UIImagePickerControllerOriginalImage] as! UIImage
        self.imgview.image = image

        self.dismiss(animated: true, completion: nil)
    

【讨论】:

我非常感谢您的回答,并且确实在另一个威胁中遇到了您之前的回答。它崩溃了:'presentViewController(:animated:completion:)' 已使用 swift 3.0 重命名为'present(:animated:completion:)' 好的,我去看看 同样的崩溃,但在“从相机胶卷中选择”。很棒的实现,喜欢它! 给我您的电子邮件 ID。我会把demo转发给你【参考方案4】:

为您的 swift 3.0 代码试试这个,

 @IBAction func takePhoto(_ sender: UIButton) 
    var picker = UIImagePickerController()
    picker.delegate = self
    picker.allowsEditing = true
    picker.sourceType = .camera
    self.present(picker, animated: true, completion:  _ in )


@IBAction func selectPhoto(_ sender: UIButton) 
    var picker = UIImagePickerController()
    picker.delegate = self
    picker.allowsEditing = true
    picker.sourceType = .photoLibrary
    self.present(picker, animated: true, completion:  _ in )


func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [AnyHashable: Any]) 
    var chosenImage = info[UIImagePickerControllerEditedImage]
    self.imageView!.image = chosenImage
    picker.dismiss(animated: true, completion:  _ in )


func imagePickerControllerDidCancel(_ picker: UIImagePickerController) 
    picker.dismiss(animated: true, completion:  _ in )

它将在您的应用程序中显示选择器,并且可以完美运行。

希望它能解决你的问题。

【讨论】:

好的,现在这很好,因为我也在 swift 2.2 中给出了答案,这就是我评论它的原因??? @PeterdeVries 检查我更新的答案。它对我有用。 @PeterdeVries 好的。没问题。检查并提供反馈【参考方案5】:

在 Swift 3 上试试这个。

@IBAction func userImage(_ sender: AnyObject)
 if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) 

            self.imagePicker.delegate = self
            self.imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary;
            self.imagePicker.allowsEditing = true

            self.present(self.imagePicker, animated: true, completion: nil)
            self.imagePicked.isUserInteractionEnabled = true

        


【讨论】:

嗨,试过了,.camera 什么都不做(模拟器中没有相机),当我将它更改为 photoLibrary 时,同样的崩溃。 目前我在我的应用程序中使用此代码,并且在 ios 10 中也支持,为什么你投反对票.. 给你任何错误的指示? 只是让你知道,我感谢你的所有帮助,我不会在这里投票给任何人 @PeterdeVries 好的,请检查我的代码,我在 swift 3 中将其用于相机胶卷【参考方案6】:

尝试将NSCameraUsageDescription 键添加到您的 PrettyApp-Info.plist 文件中并带有一些文本描述。

【讨论】:

以上是关于iOS Swift 3 UIImagePickerController 崩溃的主要内容,如果未能解决你的问题,请参考以下文章

SWIFT - UIimagepicker 将图像分配到数组中

如何在 Documents 文件夹中保存 UIImagePicker 中的 UIImage 并获取路径字符串以保存在 Swift 中的 Core Data 中

iOS 7 UIImagePicker 预览黑屏

更改 UIImagePicker 纵横比

在 iOS8 上使用 UIImagePicker 结果 URL 和 PHAsset 从“我的照片流”加载图像

在 iOS 中存储 UIImagePicker 选取的图像