繁体   English   中英

如何让(swift 3)中的图像选择器在照片库和相机之间切换回来和第4个。

[英]How to get the image picker in (swift 3) to switch back and 4th between photo gallery and camera.

此代码允许在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.
}}

你可以创建一个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)

更新到上面的答案。 它工作得很好,但有一些拼写错误 - 需要在addActions上关闭括号。 Xcode 8.2也改变了presentViewController的呈现方式。

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM