【问题标题】:I want to let users choose either taking a photo using camera or picking a photo from library(ios-swift, apple example- foodtracker)我想让用户选择使用相机拍照或从库中挑选照片(ios-swift,apple example-foodtracker)
【发布时间】:2016-08-09 18:42:57
【问题描述】:

我正在玩 ios swift 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)

}

【问题讨论】:

    标签: ios swift camera


    【解决方案1】:

    确保您使用的是导航控制器,如果不只是通过转到您的故事板嵌入它 --> 选择您的初始 ViewController 然后编辑器 -> 嵌入 -> 导航控制器

    要使用 imagePicker ,请使用 This : -

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

    如果您不知道如何提取图像,请查看此链接:Swift - UIImagePickerController - how to use it? https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImagePickerController_Class/

    【讨论】:

      猜你喜欢
      • 2012-12-18
      • 2016-08-18
      • 2017-09-08
      • 2011-09-30
      • 1970-01-01
      • 1970-01-01
      • 2013-10-01
      • 2021-01-10
      • 2017-04-11
      相关资源
      最近更新 更多