Skip to content

Instantly share code, notes, and snippets.

@yat1ma30
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yat1ma30/570bce1a0bc8af8731d1 to your computer and use it in GitHub Desktop.
Save yat1ma30/570bce1a0bc8af8731d1 to your computer and use it in GitHub Desktop.
【iOS】【Swift】写真をその場で撮ったり選択したりする ref: http://qiita.com/ottati/items/a25da15955f00c5c70e9
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBAction func addButton(sender: AnyObject) {
self.pickImageFromCamera()
self.pickImageFromLibrary()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// 写真を撮ってそれを選択
func pickImageFromCamera() {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {
let controller = UIImagePickerController()
controller.delegate = self
controller.sourceType = UIImagePickerControllerSourceType.Camera
self.presentViewController(controller, animated: true, completion: nil)
}
}
// ライブラリから写真を選択する
func pickImageFromLibrary() {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) {
let controller = UIImagePickerController()
controller.delegate = self
controller.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(controller, animated: true, completion: nil)
}
}
// 写真を選択した時に呼ばれる
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
if info[UIImagePickerControllerOriginalImage] != nil {
let image = info[UIImagePickerControllerOriginalImage] as UIImage
println(image)
}
picker.dismissViewControllerAnimated(true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment