简体   繁体   English

iOS Swift 3 UIImagePickerController崩溃

[英]iOS Swift 3 UIImagePickerController crash

I upgraded project to Swift 3 and I am getting a crash on the line (at various points/viewControllers in the project) : 我将项目升级到Swift 3,并且在行中崩溃(在项目中的各个点/ viewControllers处):

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

in the function: 在函数中:

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

The crash message in the console: 控制台中的崩溃消息:

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    

in my in info.plist I have: 在我的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>

I looked around and did see that having NSPhotoLibraryUsageDescription and NSCameraUsageDescription in info.plist solves the issue ( UIImagePickerController in Swift 3 and UIImagePickerController crashes app | Swift3, Xcode8 ), but it doesn't for me. 我环顾四周,确实发现info.plist中包含NSPhotoLibraryUsageDescription和NSCameraUsageDescription解决了该问题( Swift 3中的 UIImagePickerControllerUIImagePickerController使app | Swift3,Xcode8崩溃 ),但这并不适合我。 I did clean and build, restarted xCode, restarted Mac, etc. 我做了清理和构建,重新启动了xCode,重新启动了Mac等。

It might be worth pointing out that my console at startup says: 可能值得指出的是,我的控制台在启动时说:

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. 

but according to this thread that doesn't matter https://forums.developer.apple.com/thread/63254 但是根据这个线程没关系https://forums.developer.apple.com/thread/63254

my class has the delegates: 我班有代表:

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

Any help is much appreciated 任何帮助深表感谢

Ok, so I figured it out. 好的,我知道了。 (3 days of my life) Even though I put the Privacy keys in my info.plist, they were not actually accessed/ recognised by the app. (生命中的3天)即使我将隐私密钥放在了info.plist中,该应用程序实际上也并未对其进行访问/识别。 so I had to go top level app -> Targets -> "MY_APP" -> info and put them there as well. 因此,我必须转到顶级应用程序->目标->“ MY_APP”->信息,并将其放置在该位置。 after that, it ran. 之后,它运行了。 (Solutions from Jecky, Seggy and KAR were great, so I upvoted). (来自Jecky,Seggy和KAR的解决方案很棒,因此我投票赞成)。 obviously a bug, hope it helps someone. 显然是一个错误,希望它能对某人有所帮助。

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

Try My code 试试我的代码

It is with take picture with camera or choose from library. 用相机拍照或从图库中选择。

Just give UIImagePickerControllerDelegate,UINavigationControllerDelegate 只需给UIImagePickerControllerDelegate,UINavigationControllerDelegate

Set Property in Info.plist file Privacy - Photo Library Usage Description : Allow photo 在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)
    }

Try this for Swift 3.. 试试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

        }

}

try this for your swift 3.0 code, 尝试此操作以获取快速的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 })
}

It will present picker in your app and will work perfect. 它将在您的应用中显示选择器,并且可以完美运行。

Hope it will solve your problem. 希望它能解决您的问题。

尝试使用一些文本说明将NSCameraUsageDescription键添加到PrettyApp-Info.plist文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM