teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

コードの追加

2019/01/16 10:39

投稿

mimamo
mimamo

スコア44

title CHANGED
File without changes
body CHANGED
@@ -61,7 +61,55 @@
61
61
  }
62
62
 
63
63
  ```
64
+ ###頂いたコードを参考に記述したもの
65
+ ```Swift
66
+ extension CGRect {
67
+ func aspectFit(contentSize: CGSize, stretchble: Bool, integer: Bool) -> CGRect {
68
+ let xZoom = width / contentSize.width
69
+ let yZoom = height / contentSize.height
70
+ let zoom = stretchble ? min(xZoom, yZoom) : min(xZoom, yZoom, 1)
71
+
72
+ if integer {
73
+ let newWidth = max(Int(contentSize.width * zoom), 1)
74
+ let newHeight = max(Int(contentSize.height * zoom), 1)
75
+ let newX = Int(origin.x) + (Int(width) - newWidth) / 2
76
+ let newY = Int(origin.y) + (Int(height) - newHeight) / 2
77
+ return CGRect(x: newX, y: newY, width: newWidth, height: newHeight)
78
+ } else {
79
+ let newWidth = contentSize.width * zoom
80
+ let newHeight = contentSize.height * zoom
81
+ let newX = origin.x + (width - newWidth) / 2
82
+ let newY = origin.y + (height - newHeight) / 2
83
+ return CGRect(x: newX, y: newY, width: newWidth, height: newHeight)
84
+ }
85
+ }
86
+ }
87
+
88
+
89
+
90
+ // 写真を選んだ後に呼ばれる処理
91
+ func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
92
+ // 選択した写真を取得する
93
+ let image = info[UIImagePickerControllerOriginalImage] as! UIImage
94
+
95
+ //透過していた画像を取り戻す
96
+ getphoto.alpha = 1.0
97
+
64
98
 
99
+
100
+
101
+
102
+
103
+ getphoto.frame = CGRect(x: 0, y: 0, width: screenWidth / 4, height: screenHeight / 4).aspectFit(contentSize: image.size, stretchble: true, integer: true)
104
+ getphoto.center = CGPoint(x:screenWidth/2, y:screenHeight/2)
105
+
106
+
107
+ // ビューに表示する
108
+ self.getphoto.image = image
109
+ // 写真を選ぶビューを引っ込める
110
+ self.dismiss(animated: true)
111
+ }
112
+ ```
65
113
 
66
114
 
67
115
  ### 補足情報(FW/ツールのバージョンなど)