2016-10-08 3 views
1

Вот мой код, простой класс с представлением, созданным в раскадровке, в котором содержится кнопка для представления imagePickerView. ImagePickerView получает представлены, а затем приложение падает с libc++abi.dylib: terminating with uncaught exception of type NSExceptionНастоящее ImagePickerView вызывает сбой

import Foundation 
import UIKit 


class ImageSelectionView: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 


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

@IBAction func backButtonTapped(_ sender: AnyObject) { 
    self.navigationController?.dismiss(animated: true, completion: nil) 
} 


@IBAction func openPhotoLibrary(_ sender: AnyObject) { 
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) { 
     let imagePicker = UIImagePickerController() 
     imagePicker.delegate = self 
     imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary; 
     imagePicker.allowsEditing = true 
     present(imagePicker, animated: true, completion: nil) 
     self.present(imagePicker, animated: true, completion: nil) 
    } 
} 


func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) { 
    self.dismiss(animated: true, completion: nil); 
} 




} 

не могу понять, где это происходит не так, любая помощь будет удивительным, спасибо!

+0

Вы запросили разрешение на фотографии? – neprocker

+0

@MikeG Попробуйте мой ответ и дайте мне знать Работает он или нет? –

+0

Отметьте мой ответ. –

ответ

2

Для доступа к библиотеке фотографий, вам нужно поставить Privacy - Photo Library Usage Description в .plist файл приложения с некоторым описанием,

, как это показано на рисунке

enter image description here

И porticularly в вашем коде, вы написали кода, чтобы представить тот же самый изображениеPicker дважды, как показано ниже.

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

Поэтому я предлагаю, чтобы держать один, может быть
present(imagePicker, animated: true, completion: nil)

ИЛИ

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

Надеются, что это помогает.

Счастливый кодирования ...

+0

Проблема заключалась в вызове 'present (view)' дважды, я полностью забыл, что спасибо за указание на это. – MikeG

+0

Могу ли я подняться на голосование? – Janmenjaya

+0

Я принял ответ, какова цель повышения? – MikeG

0

Ваш синтаксис является неправильным, когда U вызова imagepicker метод делегата затем ViewController является preseted выбрать изображение Так заменить

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

С

self.presentViewController(imagePicker, animated: true, completion: nil) 

и вы не показываете изображение в своем изображении

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) { 
    "YOUR IMAGEVIEW".image = info[UIImagePickerControllerOriginalImage] as? UIImage 
    self.dismiss(animated: true, completion: nil); 
} 
0

Попробуйте мой код, ваша проблема будет легко решена.

@IBAction func openPhotoLibrary(_ sender: AnyObject) { 
     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.presentViewController(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.presentViewController(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.presentViewController(myPickerController, animated: true, completion: nil) 
     } 
     actionSheetController.addAction(choosePictureAction) 

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

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 

     "YOURIMAGEVIEW".image = info[UIImagePickerControllerOriginalImage] as? UIImage 

     self.dismissViewControllerAnimated(true, completion: nil) 
} 
Смежные вопросы