我想让用户选择使用相机拍照或从库中挑选照片(ios-swift,apple example-foodtracker)

Posted

技术标签:

【中文标题】我想让用户选择使用相机拍照或从库中挑选照片(ios-swift,apple example-foodtracker)【英文标题】:I want to let users choose either taking a photo using camera or picking a photo from library(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)


【问题讨论】:

【参考方案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/

【讨论】:

以上是关于我想让用户选择使用相机拍照或从库中挑选照片(ios-swift,apple example-foodtracker)的主要内容,如果未能解决你的问题,请参考以下文章

Android - 如何从相机捕获图像或从库中添加

拍照或从库中获取图像的权限未在 iOS 9 中显示(Xcode 7beta,Swift 2)

如何旋转从照片库中挑选的照片进行编辑

隐藏按钮直到选择图片

尝试加载时 iPhone 应用程序启动画面崩溃

让用户拍照或从 Android 中的图库中选择图像的单一意图