如何为提示从图库或相机胶卷拍照的按钮创建警报视图? [英] How to create an alertview for a button prompting to take pictures from gallery or camera roll?

查看:76
本文介绍了如何为提示从图库或相机胶卷拍照的按钮创建警报视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.那么我该如何实施这种警报,要求从画廊或相机胶卷中拍照呢?

I am using an updated version of swift. In ios9 alertview is not supported I guess. So how can I implement this alert asking to take pictures from gallery or camera roll?

推荐答案

UIAlertView仍然可以使用,但从iOS 8开始不推荐使用.显示警报的首选方法是使用UIAlertControllerUIAlertControllerStyleAlert样式.

UIAlertView still can be used but is deprecated starting from iOS 8. The preferred way to present an alert is using UIAlertController with UIAlertControllerStyleAlert style.

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

这篇关于如何为提示从图库或相机胶卷拍照的按钮创建警报视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆