Advertisement
Guest User

Untitled

a guest
May 24th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. import UIKit
  2. /*
  3. info.plistに下の項目を追加する
  4. Privacy - Photo Library Usage Description
  5. Privacy - Camera Usage Description
  6.  
  7. それぞれカメラとライブラリを使う理由を書く
  8. 「プロフィールに使うために使用します」など
  9. */
  10.  
  11. class SettingPageController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
  12.  
  13. override func viewDidLoad() {
  14. super.viewDidLoad()
  15.  
  16. let userImageSize: Int = 100
  17. let userImageView: UIImageView = UIImageView()
  18. userImageView.frame = CGRect(x: 0, y: 20, width: userImageSize, height: userImageSize)
  19. userImageView.image = UIImage(named: "適当な画像")
  20. userImageView.center.x = self.view.frame.width/2
  21. userImageView.layer.cornerRadius = CGFloat(userImageSize/2)
  22. userImageView.clipsToBounds = true
  23. self.view.addSubview(userImageView)
  24.  
  25. let userImageViewButton: UIButton = UIButton()
  26. userImageViewButton.frame = CGRect(x: 0, y: 20, width: userImageSize, height: userImageSize)
  27. userImageViewButton.center.x = self.view.frame.width/2
  28. userImageViewButton.layer.cornerRadius = CGFloat(userImageSize/2)
  29. userImageViewButton.clipsToBounds = true
  30. userImageViewButton.addTarget(self, action: #selector(self.onClickUserPlofileButton(sender:)), for: .touchUpInside)
  31. self.view.addSubview(userImageViewButton)
  32.  
  33. }
  34.  
  35. /**
  36. 画像編集するボタン
  37. - Parameters: sender: UIButton
  38. */
  39. internal func onClickUserPlofileButton(sender: UIButton) {
  40. showAlert()
  41. }
  42.  
  43. func showCameraLibrary(){
  44. if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
  45. let picker = UIImagePickerController()
  46. picker.modalPresentationStyle = UIModalPresentationStyle.fullScreen
  47. picker.delegate = self
  48. picker.sourceType = UIImagePickerControllerSourceType.camera
  49.  
  50. self.present(picker, animated: true, completion: nil)
  51. }
  52. }
  53.  
  54. func showPhotoLibrary() {
  55. if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
  56. let picker = UIImagePickerController()
  57. picker.modalPresentationStyle = UIModalPresentationStyle.popover
  58. picker.delegate = self
  59. picker.sourceType = UIImagePickerControllerSourceType.photoLibrary
  60.  
  61. if let popover = picker.popoverPresentationController {
  62. popover.sourceView = self.view
  63. popover.sourceRect = self.view.frame
  64. popover.permittedArrowDirections = UIPopoverArrowDirection.any
  65. }
  66. self.present(picker, animated: true, completion: nil)
  67. }
  68. }
  69.  
  70. func showAlert() {
  71. let alert: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
  72.  
  73. let defaultAction1: UIAlertAction = UIAlertAction(title: "写真の選択", style: UIAlertActionStyle.default, handler:{
  74. (action: UIAlertAction!) -> Void in
  75. self.showPhotoLibrary()
  76. })
  77.  
  78. let defaultAction2: UIAlertAction = UIAlertAction(title: "カメラで撮影", style: UIAlertActionStyle.default, handler:{
  79. (action: UIAlertAction!) -> Void in
  80. self.showCameraLibrary()
  81. })
  82.  
  83. let defaultAction3: UIAlertAction = UIAlertAction(title: "キャンセル", style: UIAlertActionStyle.cancel, handler:{
  84. (action: UIAlertAction!) -> Void in
  85. })
  86.  
  87. alert.addAction(defaultAction1)
  88. alert.addAction(defaultAction2)
  89. alert.addAction(defaultAction3)
  90.  
  91. present(alert, animated: true, completion: nil)
  92. }
  93.  
  94. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
  95. if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
  96. print(pickedImage.size)
  97. uploadFile(image: pickedImage)
  98. }
  99. picker.dismiss(animated: true, completion: nil)
  100. }
  101.  
  102. func uploadFile(image: UIImage){
  103.  
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement