質問編集履歴

3

修正

2019/12/02 04:42

投稿

MashisonJr.
MashisonJr.

スコア11

test CHANGED
File without changes
test CHANGED
@@ -2,266 +2,266 @@
2
2
 
3
3
  複製されないようにするにはどうしたらいいのか?を知りたい
4
4
 
5
+ ```Swift
6
+
7
+ import UIKit
8
+
9
+
10
+
11
+ //画像を選択
12
+
13
+ extension TapViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
14
+
15
+
16
+
17
+ //画像を選んだ時の処理
18
+
19
+ func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
20
+
21
+
22
+
23
+ let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
24
+
25
+
26
+
27
+ self.imageView.image = image
28
+
29
+ UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
30
+
31
+ //サイズを圧縮する
32
+
33
+ // let resizedImage = selectedImage.scale(byFactor: 0.4)
34
+
35
+
36
+
37
+ image = selectedImage
38
+
39
+
40
+
41
+ var imageHeight = image.size.height
42
+
43
+ var imageWidth = image.size.width
44
+
45
+
46
+
47
+ let navigationBarHeight = navigationController?.navigationBar.frame.height
48
+
49
+ let width = self.view.frame.width
50
+
51
+ let height = self.view.frame.height
52
+
53
+ let centerX = self.view.center.x
54
+
55
+ let centerY = self.view.center.y
56
+
57
+ let widthRatio = imageWidth
58
+
59
+ let heightRatio = imageHeight
60
+
61
+ //画像の大きさに応じてiamgeviewのサイズを変える
62
+
63
+ if imageHeight > self.view.frame.height || imageWidth > self.view.frame.width {
64
+
65
+ print("1")
66
+
67
+ imageWidth = width
68
+
69
+ imageHeight = width*heightRatio/widthRatio
70
+
71
+
72
+
73
+ } else if imageHeight > self.view.frame.height {
74
+
75
+ print("2")
76
+
77
+ imageHeight = height
78
+
79
+ imageWidth = height*widthRatio/heightRatio
80
+
81
+
82
+
83
+ } else if imageWidth > self.view.frame.width {
84
+
85
+ print("3")
86
+
87
+ imageWidth = width
88
+
89
+ imageHeight = width*heightRatio/widthRatio
90
+
91
+
92
+
93
+ } else {
94
+
95
+ }
96
+
97
+
98
+
99
+ imageView.contentMode = UIView.ContentMode.scaleToFill
100
+
101
+ imageView.frame.size = CGSize(width: imageWidth, height: imageHeight)
102
+
103
+ //画像がnavigationbarに被らないようにする
104
+
105
+ if imageHeight/2 > (height/2 - navigationBarHeight!) {
106
+
107
+ print("4")
108
+
109
+ imageView.center = CGPoint(x: centerX, y: centerY + navigationBarHeight!)
110
+
111
+ } else {
112
+
113
+ print("5")
114
+
115
+ imageView.center = CGPoint(x: centerX, y: centerY)
116
+
117
+ }
118
+
119
+
120
+
121
+ imageView.image = image
122
+
123
+
124
+
125
+ picker.dismiss(animated: true, completion: nil)
126
+
127
+ }
128
+
129
+
130
+
131
+
132
+
133
+
134
+
135
+ // 撮影がキャンセルされた時に呼ばれる
136
+
137
+ func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
138
+
139
+ picker.dismiss(animated: true, completion: nil)
140
+
141
+ }
142
+
143
+
144
+
145
+ func tappedlibrary() {
146
+
147
+ let sourceType:UIImagePickerController.SourceType = UIImagePickerController.SourceType.photoLibrary
148
+
149
+
150
+
151
+ if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary){
152
+
153
+ // インスタンスの作成
154
+
155
+ let cameraPicker = UIImagePickerController()
156
+
157
+ cameraPicker.sourceType = sourceType
158
+
159
+ cameraPicker.delegate = self
160
+
161
+ self.present(cameraPicker, animated: true, completion: nil)
162
+
163
+ }
164
+
165
+ else{
166
+
167
+ print("error")
168
+
169
+
170
+
171
+ }
172
+
173
+ }
174
+
175
+
176
+
177
+ func tappedcamera() {
178
+
179
+ let sourceType:UIImagePickerController.SourceType = UIImagePickerController.SourceType.camera
180
+
181
+ // カメラが利用可能かチェック
182
+
183
+ if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera){
184
+
185
+ // インスタンスの作成
186
+
187
+ let cameraPicker = UIImagePickerController()
188
+
189
+ cameraPicker.sourceType = sourceType
190
+
191
+ cameraPicker.delegate = self
192
+
193
+ self.present(cameraPicker, animated: true, completion: nil)
194
+
195
+
196
+
197
+ }
198
+
199
+ else{
200
+
201
+ print("error")
202
+
203
+ }
204
+
205
+ }
206
+
207
+
208
+
209
+ @IBAction func selecteImageButton(_ sender: UITapGestureRecognizer) {
210
+
211
+
212
+
213
+ //アラート表示のために
214
+
215
+ let actionSheet = UIAlertController(title: "", message: "写真の選択", preferredStyle: UIAlertController.Style.actionSheet)
216
+
217
+
218
+
219
+ let tappedcamera = UIAlertAction(title: "カメラで撮影", style: UIAlertAction.Style.default, handler: {
220
+
221
+ (action: UIAlertAction!) in
222
+
223
+ self.tappedcamera()
224
+
225
+ })
226
+
227
+
228
+
229
+ let tappedlibrary = UIAlertAction(title: "ライブラリから選択", style: UIAlertAction.Style.default, handler: {
230
+
231
+ (action: UIAlertAction!) in
232
+
233
+ self.tappedlibrary()
234
+
235
+ })
236
+
237
+
238
+
239
+ let cancel = UIAlertAction(title: "キャンセル", style: UIAlertAction.Style.cancel, handler: {
240
+
241
+ (action: UIAlertAction!) in
242
+
243
+ print("キャンセル")
244
+
245
+ })
246
+
247
+
248
+
249
+ actionSheet.addAction(tappedcamera)
250
+
251
+ actionSheet.addAction(tappedlibrary)
252
+
253
+ actionSheet.addAction(cancel)
254
+
255
+
256
+
257
+ present(actionSheet, animated: true, completion: nil)
258
+
259
+
260
+
261
+ }
262
+
263
+
264
+
265
+ }
266
+
5
267
  ```
6
-
7
-
8
-
9
- import UIKit
10
-
11
-
12
-
13
- //画像を選択
14
-
15
- extension TapViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
16
-
17
-
18
-
19
- //画像を選んだ時の処理
20
-
21
- func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
22
-
23
-
24
-
25
- let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
26
-
27
-
28
-
29
- self.imageView.image = image
30
-
31
- UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
32
-
33
- //サイズを圧縮する
34
-
35
- // let resizedImage = selectedImage.scale(byFactor: 0.4)
36
-
37
-
38
-
39
- image = selectedImage
40
-
41
-
42
-
43
- var imageHeight = image.size.height
44
-
45
- var imageWidth = image.size.width
46
-
47
-
48
-
49
- let navigationBarHeight = navigationController?.navigationBar.frame.height
50
-
51
- let width = self.view.frame.width
52
-
53
- let height = self.view.frame.height
54
-
55
- let centerX = self.view.center.x
56
-
57
- let centerY = self.view.center.y
58
-
59
- let widthRatio = imageWidth
60
-
61
- let heightRatio = imageHeight
62
-
63
- //画像の大きさに応じてiamgeviewのサイズを変える
64
-
65
- if imageHeight > self.view.frame.height || imageWidth > self.view.frame.width {
66
-
67
- print("1")
68
-
69
- imageWidth = width
70
-
71
- imageHeight = width*heightRatio/widthRatio
72
-
73
-
74
-
75
- } else if imageHeight > self.view.frame.height {
76
-
77
- print("2")
78
-
79
- imageHeight = height
80
-
81
- imageWidth = height*widthRatio/heightRatio
82
-
83
-
84
-
85
- } else if imageWidth > self.view.frame.width {
86
-
87
- print("3")
88
-
89
- imageWidth = width
90
-
91
- imageHeight = width*heightRatio/widthRatio
92
-
93
-
94
-
95
- } else {
96
-
97
- }
98
-
99
-
100
-
101
- imageView.contentMode = UIView.ContentMode.scaleToFill
102
-
103
- imageView.frame.size = CGSize(width: imageWidth, height: imageHeight)
104
-
105
- //画像がnavigationbarに被らないようにする
106
-
107
- if imageHeight/2 > (height/2 - navigationBarHeight!) {
108
-
109
- print("4")
110
-
111
- imageView.center = CGPoint(x: centerX, y: centerY + navigationBarHeight!)
112
-
113
- } else {
114
-
115
- print("5")
116
-
117
- imageView.center = CGPoint(x: centerX, y: centerY)
118
-
119
- }
120
-
121
-
122
-
123
- imageView.image = image
124
-
125
-
126
-
127
- picker.dismiss(animated: true, completion: nil)
128
-
129
- }
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
- // 撮影がキャンセルされた時に呼ばれる
138
-
139
- func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
140
-
141
- picker.dismiss(animated: true, completion: nil)
142
-
143
- }
144
-
145
-
146
-
147
- func tappedlibrary() {
148
-
149
- let sourceType:UIImagePickerController.SourceType = UIImagePickerController.SourceType.photoLibrary
150
-
151
-
152
-
153
- if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary){
154
-
155
- // インスタンスの作成
156
-
157
- let cameraPicker = UIImagePickerController()
158
-
159
- cameraPicker.sourceType = sourceType
160
-
161
- cameraPicker.delegate = self
162
-
163
- self.present(cameraPicker, animated: true, completion: nil)
164
-
165
- }
166
-
167
- else{
168
-
169
- print("error")
170
-
171
-
172
-
173
- }
174
-
175
- }
176
-
177
-
178
-
179
- func tappedcamera() {
180
-
181
- let sourceType:UIImagePickerController.SourceType = UIImagePickerController.SourceType.camera
182
-
183
- // カメラが利用可能かチェック
184
-
185
- if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera){
186
-
187
- // インスタンスの作成
188
-
189
- let cameraPicker = UIImagePickerController()
190
-
191
- cameraPicker.sourceType = sourceType
192
-
193
- cameraPicker.delegate = self
194
-
195
- self.present(cameraPicker, animated: true, completion: nil)
196
-
197
-
198
-
199
- }
200
-
201
- else{
202
-
203
- print("error")
204
-
205
- }
206
-
207
- }
208
-
209
-
210
-
211
- @IBAction func selecteImageButton(_ sender: UITapGestureRecognizer) {
212
-
213
-
214
-
215
- //アラート表示のために
216
-
217
- let actionSheet = UIAlertController(title: "", message: "写真の選択", preferredStyle: UIAlertController.Style.actionSheet)
218
-
219
-
220
-
221
- let tappedcamera = UIAlertAction(title: "カメラで撮影", style: UIAlertAction.Style.default, handler: {
222
-
223
- (action: UIAlertAction!) in
224
-
225
- self.tappedcamera()
226
-
227
- })
228
-
229
-
230
-
231
- let tappedlibrary = UIAlertAction(title: "ライブラリから選択", style: UIAlertAction.Style.default, handler: {
232
-
233
- (action: UIAlertAction!) in
234
-
235
- self.tappedlibrary()
236
-
237
- })
238
-
239
-
240
-
241
- let cancel = UIAlertAction(title: "キャンセル", style: UIAlertAction.Style.cancel, handler: {
242
-
243
- (action: UIAlertAction!) in
244
-
245
- print("キャンセル")
246
-
247
- })
248
-
249
-
250
-
251
- actionSheet.addAction(tappedcamera)
252
-
253
- actionSheet.addAction(tappedlibrary)
254
-
255
- actionSheet.addAction(cancel)
256
-
257
-
258
-
259
- present(actionSheet, animated: true, completion: nil)
260
-
261
-
262
-
263
- }
264
-
265
-
266
-
267
- }```

2

修正

2019/12/02 04:41

投稿

MashisonJr.
MashisonJr.

スコア11

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  複製されないようにするにはどうしたらいいのか?を知りたい
4
4
 
5
- ```Swift
5
+ ```
6
6
 
7
7
 
8
8
 

1

修正

2019/12/02 04:40

投稿

MashisonJr.
MashisonJr.

スコア11

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,267 @@
1
1
  Swiftでライブラリの画像を選択したらその画像が複製される問題を解決したい
2
2
 
3
3
  複製されないようにするにはどうしたらいいのか?を知りたい
4
+
5
+ ```Swift
6
+
7
+
8
+
9
+ import UIKit
10
+
11
+
12
+
13
+ //画像を選択
14
+
15
+ extension TapViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
16
+
17
+
18
+
19
+ //画像を選んだ時の処理
20
+
21
+ func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
22
+
23
+
24
+
25
+ let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
26
+
27
+
28
+
29
+ self.imageView.image = image
30
+
31
+ UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
32
+
33
+ //サイズを圧縮する
34
+
35
+ // let resizedImage = selectedImage.scale(byFactor: 0.4)
36
+
37
+
38
+
39
+ image = selectedImage
40
+
41
+
42
+
43
+ var imageHeight = image.size.height
44
+
45
+ var imageWidth = image.size.width
46
+
47
+
48
+
49
+ let navigationBarHeight = navigationController?.navigationBar.frame.height
50
+
51
+ let width = self.view.frame.width
52
+
53
+ let height = self.view.frame.height
54
+
55
+ let centerX = self.view.center.x
56
+
57
+ let centerY = self.view.center.y
58
+
59
+ let widthRatio = imageWidth
60
+
61
+ let heightRatio = imageHeight
62
+
63
+ //画像の大きさに応じてiamgeviewのサイズを変える
64
+
65
+ if imageHeight > self.view.frame.height || imageWidth > self.view.frame.width {
66
+
67
+ print("1")
68
+
69
+ imageWidth = width
70
+
71
+ imageHeight = width*heightRatio/widthRatio
72
+
73
+
74
+
75
+ } else if imageHeight > self.view.frame.height {
76
+
77
+ print("2")
78
+
79
+ imageHeight = height
80
+
81
+ imageWidth = height*widthRatio/heightRatio
82
+
83
+
84
+
85
+ } else if imageWidth > self.view.frame.width {
86
+
87
+ print("3")
88
+
89
+ imageWidth = width
90
+
91
+ imageHeight = width*heightRatio/widthRatio
92
+
93
+
94
+
95
+ } else {
96
+
97
+ }
98
+
99
+
100
+
101
+ imageView.contentMode = UIView.ContentMode.scaleToFill
102
+
103
+ imageView.frame.size = CGSize(width: imageWidth, height: imageHeight)
104
+
105
+ //画像がnavigationbarに被らないようにする
106
+
107
+ if imageHeight/2 > (height/2 - navigationBarHeight!) {
108
+
109
+ print("4")
110
+
111
+ imageView.center = CGPoint(x: centerX, y: centerY + navigationBarHeight!)
112
+
113
+ } else {
114
+
115
+ print("5")
116
+
117
+ imageView.center = CGPoint(x: centerX, y: centerY)
118
+
119
+ }
120
+
121
+
122
+
123
+ imageView.image = image
124
+
125
+
126
+
127
+ picker.dismiss(animated: true, completion: nil)
128
+
129
+ }
130
+
131
+
132
+
133
+
134
+
135
+
136
+
137
+ // 撮影がキャンセルされた時に呼ばれる
138
+
139
+ func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
140
+
141
+ picker.dismiss(animated: true, completion: nil)
142
+
143
+ }
144
+
145
+
146
+
147
+ func tappedlibrary() {
148
+
149
+ let sourceType:UIImagePickerController.SourceType = UIImagePickerController.SourceType.photoLibrary
150
+
151
+
152
+
153
+ if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary){
154
+
155
+ // インスタンスの作成
156
+
157
+ let cameraPicker = UIImagePickerController()
158
+
159
+ cameraPicker.sourceType = sourceType
160
+
161
+ cameraPicker.delegate = self
162
+
163
+ self.present(cameraPicker, animated: true, completion: nil)
164
+
165
+ }
166
+
167
+ else{
168
+
169
+ print("error")
170
+
171
+
172
+
173
+ }
174
+
175
+ }
176
+
177
+
178
+
179
+ func tappedcamera() {
180
+
181
+ let sourceType:UIImagePickerController.SourceType = UIImagePickerController.SourceType.camera
182
+
183
+ // カメラが利用可能かチェック
184
+
185
+ if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera){
186
+
187
+ // インスタンスの作成
188
+
189
+ let cameraPicker = UIImagePickerController()
190
+
191
+ cameraPicker.sourceType = sourceType
192
+
193
+ cameraPicker.delegate = self
194
+
195
+ self.present(cameraPicker, animated: true, completion: nil)
196
+
197
+
198
+
199
+ }
200
+
201
+ else{
202
+
203
+ print("error")
204
+
205
+ }
206
+
207
+ }
208
+
209
+
210
+
211
+ @IBAction func selecteImageButton(_ sender: UITapGestureRecognizer) {
212
+
213
+
214
+
215
+ //アラート表示のために
216
+
217
+ let actionSheet = UIAlertController(title: "", message: "写真の選択", preferredStyle: UIAlertController.Style.actionSheet)
218
+
219
+
220
+
221
+ let tappedcamera = UIAlertAction(title: "カメラで撮影", style: UIAlertAction.Style.default, handler: {
222
+
223
+ (action: UIAlertAction!) in
224
+
225
+ self.tappedcamera()
226
+
227
+ })
228
+
229
+
230
+
231
+ let tappedlibrary = UIAlertAction(title: "ライブラリから選択", style: UIAlertAction.Style.default, handler: {
232
+
233
+ (action: UIAlertAction!) in
234
+
235
+ self.tappedlibrary()
236
+
237
+ })
238
+
239
+
240
+
241
+ let cancel = UIAlertAction(title: "キャンセル", style: UIAlertAction.Style.cancel, handler: {
242
+
243
+ (action: UIAlertAction!) in
244
+
245
+ print("キャンセル")
246
+
247
+ })
248
+
249
+
250
+
251
+ actionSheet.addAction(tappedcamera)
252
+
253
+ actionSheet.addAction(tappedlibrary)
254
+
255
+ actionSheet.addAction(cancel)
256
+
257
+
258
+
259
+ present(actionSheet, animated: true, completion: nil)
260
+
261
+
262
+
263
+ }
264
+
265
+
266
+
267
+ }```