[Swift]Swift #4 カメラを扱う
Swift #4 カメラを扱う
ViewController
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate { @IBOutlet var mainImageView: UIImageView var picker:UIImagePickerController? override func viewDidLoad() { super.viewDidLoad() picker = UIImagePickerController() picker!.delegate = self picker!.allowsEditing = false } @IBAction func onTapButton1(button:UIButton){ println( "onTapButton - open album" ) if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) { picker!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary self.presentViewController(picker, animated: true, completion: nil) } } @IBAction func onTapButton2(button:UIButton){ println( "onTapButton - take photo" ) if picker==nil { return } if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) { picker!.sourceType = UIImagePickerControllerSourceType.Camera self.presentViewController(picker, animated: true, completion: nil) } } func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){ println( "imagePickerController" ) self.dismissViewControllerAnimated(true, completion: nil) mainImageView.image = image if picker!.sourceType == UIImagePickerControllerSourceType.Camera { println( "save photo" ) UIImageWriteToSavedPhotosAlbum(image, self, "onSaveImageWithUIImage:error:contextInfo:", nil) } } func onSaveImageWithUIImage(image: UIImage!, error: NSErrorPointer, contextInfo: UnsafePointer<()>){ if error { println( "error" ) return } println( "success" ) } func imagePickerControllerDidCancel(picker: UIImagePickerController!){ println( "imagePickerControllerDidCancel" ) self.dismissViewControllerAnimated(true, completion: nil) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }