Load Camera or PhotoLibrary with UIImagePickerController? [duplicate]

时光怂恿深爱的人放手 提交于 2020-01-06 15:52:16

问题


I have recently put in code to either take a picture, or select a picture, in Swift 3 on iOS 10. When calling either open camera or photo library, my app crashes with a "Thread 14: signal SIGABRT" with no other indication of what's happening. To me, and from what I've debugged, is this happens when trying to load the native camera view or photo library. Any help would be much appreciated!

Code below:

class CameraViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

@IBOutlet weak var imageView: UIImageView!


var imagePicker = UIImagePickerController()

override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}


@IBAction func BTN_takePhoto(_ sender: AnyObject) {

    print("button pressed")

    if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)){

        print("source available")
        //imagePicker =  UIImagePickerController()
        print(1)
        imagePicker.delegate = self
        print(2)
        imagePicker.sourceType = UIImagePickerControllerSourceType.camera
        print(3)
        imagePicker.mediaTypes = [kUTTypeImage as String]
        print(4)
        imagePicker.allowsEditing = true
        print(5)
        self.present(imagePicker, animated: true, completion: nil)
        print(6)
    }
    else {
        print("ERROR: source not available")
        NSLog("No Camera")
        let alert = UIAlertController(title: "No camera", message: "Please allow this app the use of your camera in settings or buy a device that has a camera.", preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}

@IBAction func BTN_pickFromLibrary(_ sender: AnyObject) {

    imagePicker.allowsEditing = false //2
    imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary //3
    present(imagePicker, animated: true, completion: nil)//4


}


func imagePickerController(imagePicker: UIImagePickerController, didFinishPickingMediaWithInfo info: NSDictionary!) {
    NSLog("Received image from camera")
    let mediaType = info[UIImagePickerControllerMediaType] as! String
    var originalImage:UIImage?, editedImage:UIImage?, imageToSave:UIImage?
    let compResult:CFComparisonResult = CFStringCompare(mediaType as NSString!, kUTTypeImage, CFStringCompareFlags.compareCaseInsensitive)
    if ( compResult == CFComparisonResult.compareEqualTo ) {

        editedImage = info[UIImagePickerControllerEditedImage] as! UIImage?
        originalImage = info[UIImagePickerControllerOriginalImage] as! UIImage?

        if ( editedImage != nil ) {
            imageToSave = editedImage
        } else {
            imageToSave = originalImage
        }
        imageView.image = imageToSave
        imageView.reloadInputViews()
    }
    imagePicker.dismiss(animated: true, completion: nil)
}

func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    imagePicker.dismiss(animated: true, completion: nil)
}


/*
private func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    imagePicker.dismiss(animated: true, completion: nil)
    imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
*/

}


回答1:


You have to add a key in Info.plist now from iOS 10 onwards

<key>NSPhotoLibraryUsageDescription</key>
<string>Use Photos</string>

Check this link for more details



来源:https://stackoverflow.com/questions/40011536/load-camera-or-photolibrary-with-uiimagepickercontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!