【问题标题】:How to get the image picker in (swift 3) to switch back and 4th between photo gallery and camera.如何让图像选择器(swift 3)在照片库和相机之间切换回来和第四次。
【发布时间】:2017-02-10 23:25:18
【问题描述】:

此代码允许在 Xcode 中访问我在 iPhone 模拟器上的照片库。当我把它放在我的 iPad 上时,我可以访问相机,但不能访问照片库。如果选择,我希望图像视图显示相机照片或从照片库加载图像(如果选择)。

import UIKit

 class ViewController:   UIViewController,   UIImagePickerControllerDelegate, UINavigationControllerDelegate {

let picker = UIImagePickerController()
@IBOutlet weak var iv: UIImageView!

@IBAction func cameraf(_ sender: UIBarButtonItem) {

    if UIImagePickerController.isSourceTypeAvailable(.camera) {
        picker.allowsEditing = false
        picker.sourceType = UIImagePickerControllerSourceType.camera
        picker.cameraCaptureMode = .photo
        picker.modalPresentationStyle = .fullScreen
        present(picker,animated: true,completion: nil)
    } else {
        noCamera()
    }}

@IBAction func photolibaryss(_ sender: UIBarButtonItem) {
    picker.allowsEditing = false
    picker.sourceType = .photoLibrary
    picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
    picker.modalPresentationStyle = .popover
    present(picker, animated: true, completion: nil)
    picker.popoverPresentationController?.barButtonItem = sender
}

func noCamera(){
    let alertVC = UIAlertController(
        title: "No Camera",
        message: "Sorry, this device has no camera",
        preferredStyle: .alert)
    let okAction = UIAlertAction(
        title: "OK",
        style:.default,
        handler: nil)
    alertVC.addAction(okAction)
    present(
        alertVC,
        animated: true,
        completion: nil)
}
override func viewDidLoad() {
    super.viewDidLoad()
    picker.delegate = self
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
    var  chosenImage = UIImage()
    chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
    iv.contentMode = .scaleAspectFit
    iv.image = chosenImage
    dismiss(animated:true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    dismiss(animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}}

【问题讨论】:

    标签: ios image camera uiimagepickercontroller swift3


    【解决方案1】:

    更新上面的答案。它工作得很好,但有一些拼写错误 - 需要在 addActions 上加上右括号。 Xcode 8.2 也将 presentViewController 更改为 present。

    let alert = UIAlertController(title: nil, message: "Choose your source", preferredStyle: UIAlertControllerStyle.Alert)
    
    alert.addAction(UIAlertAction(title: "Camera", style: UIAlertActionStyle.Default) { (result : UIAlertAction) -> Void in
            print("Camera selected")
            //Code for Camera
            //cameraf 
        })
    alert.addAction(UIAlertAction(title: "Photo library", style: UIAlertActionStyle.Default) { (result : UIAlertAction) -> Void in
            print("Photo selected")
            //Code for Photo library
            //photolibaryss
        })
    self.present(alert, animated: true, completion: nil)
    

    【讨论】:

      【解决方案2】:

      您可以制作一个 UIAlertController,它由两个警报动作照片库和相机组成,然后将 photolibaryss 和 cameraf 的功能代码行添加到相应的警报动作中。

      let alert = UIAlertController(title: nil, message: "Choose your source", preferredStyle: UIAlertControllerStyle.Alert)

      alert.addAction(UIAlertAction(title: "Camera", style: UIAlertActionStyle.Default) { (result : UIAlertAction) -> Void in
              print("Camera selected")
              //Code for Camera
              //cameraf 
          })
      alert.addAction(UIAlertAction(title: "Photo library", style: UIAlertActionStyle.Default) { (result : UIAlertAction) -> Void in
              print("Photo selected")
              //Code for Photo library
              //photolibaryss
          })
      self.present(alert, animated: true, completion: nil)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-23
        • 2011-09-07
        • 2022-01-18
        • 1970-01-01
        • 2023-03-03
        • 1970-01-01
        • 2019-02-23
        • 1970-01-01
        相关资源
        最近更新 更多