2016-10-08 15 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

이 사진 라이브러리에 액세스하기위한, 당신은 당신의 코드에서 그림과 같이 porticularly

enter image description here

그리고 몇 가지 설명과 함께 응용 프로그램의 .plist 파일에 Privacy - Photo Library Usage Description,

을 둘 필요가, 당신이 쓴 아래 그림과 같이 동일한 imagePicker를 두 번 표시하는 코드입니다.

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

는 그러므로 나는

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


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

또는

는 희망이 도움이 될 수있다, 하나를 유지하는 것이 좋습니다.

해피 코딩 ...

+0

문제는'present (view)'를 두 번 호출하는 것이 었습니다. 나는 그것을 지적 해 주셔서 감사합니다. – MikeG

+0

투표 할 수 있습니까? – Janmenjaya

+0

나는 대답을 받았다. upvote의 목적은 무엇인가? – MikeG

0

U 그래서

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

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

와에서 대체 imagepicker의 대리자 메서드를 다음의 ViewController 이미지를 선택하기 위해 미리 조정되어 호출 할 때 귀하의 구문이 잘못되었습니다 이미지 뷰에 이미지가 표시되지 않습니다.

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