如何为提示从图库或相机胶卷拍照的按钮创建警报视图?

Posted

技术标签:

【中文标题】如何为提示从图库或相机胶卷拍照的按钮创建警报视图?【英文标题】:How to create an alertview for a button prompting to take pictures from gallery or camera roll? 【发布时间】:2015-12-31 13:01:41 【问题描述】:
import UIKit

class AccountViewController: UIViewController,UINavigationControllerDelegate, UIImagePickerControllerDelegate 

@IBOutlet weak var imageView: UIImageView!

@IBOutlet weak var chooseButton: UIButton!

let imagePicker = UIImagePickerController()

@IBOutlet weak var mySwitch: UISwitch!
@IBOutlet weak var myStatus: UILabel!
@IBAction func mySwitchTapped(sender: AnyObject) 

 updateSwitchStatus()


override func viewDidLoad() 
    super.viewDidLoad()
    updateSwitchStatus()

    self.navigationController!.navigationBarHidden = true
    self.view.backgroundColor = UIColor.whiteColor()

    // Do any additional setup after loading the view.


override func didReceiveMemoryWarning() 
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.


func updateSwitchStatus()

    if mySwitch.on

        myStatus.textColor = UIColor(colorLiteralRed: 0.142, green: 0.742, blue: 0.146, alpha: 1.00)
       myStatus.text = "I'M ONLINE"

        
    else

        myStatus.textColor = UIColor(colorLiteralRed: 0.896, green: 0.085, blue: 0.000, alpha: 1.00)
        myStatus.text = "I'M OFFLINE"
    

@IBAction func shootPhoto() 
    imagePicker.allowsEditing = false
    imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
    imagePicker.cameraCaptureMode = .Photo
    imagePicker.modalPresentationStyle = .FullScreen
    presentViewController(imagePicker,
        animated: true,
        completion: nil)


   @IBAction func btnClicked()

    if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum))
        print("Button capture")
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum;
        imagePicker.allowsEditing = false

        self.presentViewController(imagePicker, animated: true, completion: nil)
    


func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: NSDictionary) 
    self.dismissViewControllerAnimated(true, completion: ()-> Void in

        )

    imageView.image = image


我正在使用 swift 的更新版本。我猜不支持在 ios9 alertview 中。那么如何实现这个要求从图库或相机胶卷中拍照的警报呢?

【问题讨论】:

【参考方案1】:

UIAlertView 仍然可以使用,但从 iOS 8 开始已弃用。呈现警报的首选方式是使用 UIAlertControllerUIAlertControllerStyleAlert 样式。

    let alertController = UIAlertController(title: "Alert title", message: "Alert Message", preferredStyle: .Alert)

    let cameraRollAction = UIAlertAction(title: "Camera Roll", style: .Default)  (action) in
        self.imagePicker.delegate = self
        self.imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum;
        self.imagePicker.allowsEditing = false
        self.presentViewController(self.imagePicker, animated: true, completion: nil)
    
    alertController.addAction(cameraRollAction)

    let takePictureAction = UIAlertAction(title: "Take a picture", style: .Default)  (action) in
        self.imagePicker.allowsEditing = false
        self.imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
        self.imagePicker.cameraCaptureMode = .Photo
        self.imagePicker.modalPresentationStyle = .FullScreen
        self.presentViewController(self.imagePicker,
            animated: true,
            completion: nil)
    
    alertController.addAction(takePictureAction)

    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel)  (action) in
    
    alertController.addAction(cancelAction)

    self.presentViewController(alertController, animated: true) 
    

【讨论】:

图像未从相机胶卷加载。@azimov 我已经检查过了,它应该可以工作。您是否允许应用访问它? 它现在正在工作.. 非常感谢 @azimov

以上是关于如何为提示从图库或相机胶卷拍照的按钮创建警报视图?的主要内容,如果未能解决你的问题,请参考以下文章

从图库或相机返回后不调用 onActivityResult

UIImagePicker 设置多个图像视图

将相机图像上传到 Firebase

如何在 ImageView 中显示图库中的图像?

Android - 如何放置一个从 SD 卡或相机胶卷加载图像的图像视图?

如何在 Swift 中按名称/标识符从相机胶卷中获取图像