2016-08-09 1 views
1

私はios swift foodtrackerの例で遊んでいる。 デフォルトの最終的な例では、ユーザーがライブラリから写真を選択できるようにしています。 しかし、カメラを使って写真を撮ってもらいたいです。 以下のコードを試しましたが、機能は変更されませんでした。ユーザーのオプション設定(カメラ/フォトライブラリ)を尋ねることなく、まだ写真ライブラリに行きました。私はユーザーがカメラを使って写真を撮るか、図書館(ios-swift、apple example- foodtracker)から写真を選ぶかを選択できるようにしたい。

私は次のエラーメッセージ取得しています:それは割り当て解除されている間、ビューコントローラのビューをロードしようとすると

は(許可されていないと、未定義の動作を引き起こす場合があります)

そして、これを私が使用したコードは次のとおりです。


@IBAction func selectImageFromPhotoLibrary(sender: AnyObject) { 
    // Hide the keyboard. 
    nameTextField.resignFirstResponder() 


    // UIImagePickerController is a view controller that lets a user pick media from their photo library. 
    let imagePickerController = UIImagePickerController() 
    imagePickerController.delegate = self 

    let alert = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet) 
    alert.addAction(UIAlertAction(title: "Camera", style: .Default, handler: { 
     action in 
     imagePickerController.sourceType = .Camera 
     self.presentViewController(imagePickerController, animated: true, completion: nil) 
    })) 
    alert.addAction(UIAlertAction(title: "Photo Library", style: .Default, handler: { 
     action in 
     imagePickerController.sourceType = .PhotoLibrary 
     self.presentViewController(imagePickerController, animated: true, completion: nil) 
    })) 



    alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)) 
    presentViewController(imagePickerController, animated: true, completion: nil) 

} 

答えて

0

suコマンドを作ります

class ViewController : UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{ 


..... 
..... 
..... 

var imagePicker : UIImagePickerController! 
//Defined it globally in the class 
.. 
.. 
    override func viewDidLoad() { 

    super.viewDidLoad() 
    imagePicker = UIImagePickerController() 
    self.imagePicker.delegate = self 
    //Initialise it in viewDidLoad 
    ... 
    } 
    //In your 'selectImageFromPhotoLibrary' function : - 

@IBAction func selectImageFromPhotoLibrary(sender: AnyObject) { 

    let alertController : UIAlertController = UIAlertController(title: "Camera/Photo Album ", message: "", preferredStyle: UIAlertControllerStyle.ActionSheet) 

    let cameraAction : UIAlertAction = UIAlertAction(title: "Camera", style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in 

       if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)) { 
        self.imagePicker.sourceType = UIImagePickerControllerSourceType.Camera 
       } else { 
         print("Camera not available") 
       } 
      self.present() 
     } 

    alertController.addAction(cameraAction) 

      let photoLibraryAction : UIAlertAction = UIAlertAction(title: "Photo Library", style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in 


      if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary)) { 
       self.imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary 
      } else { 
       print("Photo Library not available") 
      } 
    self.present() 
    } 

    alertController.addAction(photoLibraryAction) 

    alertController.popoverPresentationController?.sourceView = view 

    alertController.popoverPresentationController?.sourceRect = view.frame 

    presentViewController(alertController, animated: true, completion: nil) 

    } 


... 
... 


// After you are done picking up image , this function will be automatically be called. 
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 

    print("info reached") 

    imagePicker.dismissViewControllerAnimated(true, completion: nil) 
    //Now do whatever you want to do with your picked image. 


    } 

//For presenting the imagePicker Controller. 
func present(){ 
self.presentViewController(self.imagePicker, animated: true, completion: nil) 

} 



} 
- :これを使用し、imagePickerを使用するには>ナビゲーションコントローラ

- >埋め込まで - >あなたの初期のViewControllerその後、エディタを選択します - だけでなく、あなたのストーリーボードに行くことによって、それを埋め込む場合は、ナビゲーションコントローラを使用している再

あなたが画像を抽出する方法で立ち往生した場合だけ、このリンクをチェックアウト:Swift - UIImagePickerController - how to use it? https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImagePickerController_Class/

関連する問題