Guest User

Untitled

a guest
Feb 15th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. import UIKit
  2.  
  3. class PostViewController : UIViewController {
  4.  
  5. @IBOutlet weak var userImageView: UIImageView!
  6.  
  7. @IBOutlet weak var textView: UITextView!
  8.  
  9. // 「カメラを起動する」がタップされた.
  10. @IBAction func onTapCamera(_ sender: Any) {
  11.  
  12. // カメラが起動できるかをチェックします.
  13. if UIImagePickerController.isSourceTypeAvailable(.camera) == false {
  14. self.showAlert(message: "カメラは利用できません")
  15. return
  16. }
  17.  
  18. // カメラを起動します.
  19. let picker = UIImagePickerController()
  20. picker.sourceType = .camera
  21. picker.delegate = self
  22. self.present(picker, animated: true)
  23.  
  24. }
  25.  
  26. // 「写真を選ぶ」がタップされた.
  27. @IBAction func onTapPhoto(_ sender: Any) {
  28.  
  29. // アルバムが利用可能かをチェックします.
  30. if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) == false {
  31. self.showAlert(message: "アルバムは利用できません。")
  32. return
  33. }
  34.  
  35. // アルバムを起動します.
  36. let picker = UIImagePickerController()
  37. picker.sourceType = .photoLibrary
  38. picker.delegate = self
  39. self.present(picker, animated: true)
  40. }
  41.  
  42. @IBAction func onTapPost(_ sender: Any) {
  43. }
  44.  
  45.  
  46. }
  47.  
  48. // UIImagePickerController で必要なので実装します(具体的な処理は不要なので中身はない).
  49. extension PostViewController: UINavigationControllerDelegate {}
  50.  
  51. // カメラまたは写真で、画像が選択された時などの処理を実装する.
  52. extension PostViewController: UIImagePickerControllerDelegate {
  53.  
  54. // カメラまたは写真で、画像が選択された.
  55. func imagePickerController(_ picker: UIImagePickerController,
  56. didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
  57.  
  58. // ピッカーを閉じる.
  59. picker.dismiss(animated: true, completion: nil)
  60.  
  61. // ユーザーが撮影した or 選んだ、画像を取得する.
  62. if let image = info[.originalImage] as? UIImage {
  63.  
  64. // このアプリでは、正方形に勝手にクロップする.
  65. let croppedImage = self.cropRect(image: image)
  66.  
  67. // 画面に表示する.
  68. self.userImageView.image = croppedImage
  69. }
  70. }
  71. }
  72.  
  73. // ユーティリティ系.
  74. extension PostViewController {
  75.  
  76. // 画像を正方形にクロップする.
  77. private func cropRect(image: UIImage) -> UIImage {
  78.  
  79. // 加工対象の画像.
  80. var image = image
  81.  
  82. // 天地が反転している場合があるので、対応しておく.
  83. UIGraphicsBeginImageContext(image.size)
  84. image.draw(in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
  85. if let _image = UIGraphicsGetImageFromCurrentImageContext() {
  86. image = _image
  87. }
  88. UIGraphicsEndImageContext()
  89.  
  90. // 正方形にクロップする.
  91. if image.size.width != image.size.height {
  92. var x: CGFloat = 0
  93. var y: CGFloat = 0
  94. var w = image.size.width
  95. var h = image.size.height
  96. if w > h {
  97. // 横長の場合.
  98. x = (w - h) / 2
  99. w = h
  100. } else {
  101. // 縦長の場合.
  102. y = (h - w) / 2
  103. h = w
  104. }
  105. let cgImage = image.cgImage?.cropping(to: CGRect(x: x, y: y, width: w, height: h))
  106. image = UIImage(cgImage: cgImage!)
  107. }
  108.  
  109. // サイズが大きすぎても困るので、小さくしておく.
  110. let newSize = CGSize(width: 720, height: 720)
  111. UIGraphicsBeginImageContext(newSize)
  112. image.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
  113. if let _image = UIGraphicsGetImageFromCurrentImageContext() {
  114. image = _image
  115. }
  116. UIGraphicsEndImageContext()
  117.  
  118. // 返却する.
  119. return image
  120. }
  121. }
Add Comment
Please, Sign In to add comment