Last active
August 29, 2015 14:08
-
-
Save yat1ma30/570bce1a0bc8af8731d1 to your computer and use it in GitHub Desktop.
【iOS】【Swift】写真をその場で撮ったり選択したりする ref: http://qiita.com/ottati/items/a25da15955f00c5c70e9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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