繁体   English   中英

iOS Swift 3 UIImagePickerController崩溃

[英]iOS Swift 3 UIImagePickerController crash

我将项目升级到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解决了该问题( Swift 3中的 UIImagePickerControllerUIImagePickerController使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 

任何帮助深表感谢

好的,我知道了。 (生命中的3天)即使我将隐私密钥放在了info.plist中,该应用程序实际上也并未对其进行访问/识别。 因此,我必须转到顶级应用程序->目标->“ MY_APP”->信息,并将其放置在该位置。 之后,它运行了。 (来自Jecky,Seggy和KAR的解决方案很棒,因此我投票赞成)。 显然是一个错误,希望它能对某人有所帮助。

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

试试我的代码

用相机拍照或从图库中选择。

只需给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)
    }

试试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

        }

}

尝试此操作以获取快速的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 })
}

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

希望它能解决您的问题。

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

暂无
暂无

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

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