質問編集履歴
1
コードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,6 +1,4 @@
|
|
1
|
-
swiftでARKitを使って、カメラロール
|
1
|
+
swiftでARKitを使って、カメラロールで取得した写真を拡大縮小できるような機能を実装しています。
|
2
|
-
|
3
|
-
2つのオープンソースをつなげているのですが、SCNNodeの型が違う等のエラーが発生しました。
|
4
2
|
|
5
3
|
|
6
4
|
|
@@ -8,22 +6,12 @@
|
|
8
6
|
|
9
7
|
|
10
8
|
|
9
|
+
UIPinchGestureRecognizerのターゲットを変更し、コード上のエラーは出ず、実装できたのですが、写真を空間上に映し出せても、ピンチするとエラーが発生し、アプリがクラッシュします。
|
10
|
+
|
11
|
+
|
12
|
+
|
11
13
|
```
|
12
14
|
|
13
|
-
エラーメッセージ
|
14
|
-
|
15
|
-
●Cannot convert value of type '(UIImage, SCNVector3) -> SCNNode' to expected argument type 'SCNNode'
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
●Value of type '(UIImage, SCNVector3) -> SCNNode' has no member 'scale'
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
//createPhotoNodeを使う場所でエラーが発生します。
|
24
|
-
|
25
|
-
|
26
|
-
|
27
15
|
該当のソースコード
|
28
16
|
|
29
17
|
|
@@ -44,6 +32,8 @@
|
|
44
32
|
|
45
33
|
|
46
34
|
|
35
|
+
|
36
|
+
|
47
37
|
let defaultConfigulation: ARWorldTrackingConfiguration = {
|
48
38
|
|
49
39
|
let configuration = ARWorldTrackingConfiguration()
|
@@ -62,6 +52,8 @@
|
|
62
52
|
|
63
53
|
|
64
54
|
|
55
|
+
|
56
|
+
|
65
57
|
override func viewDidLoad() {
|
66
58
|
|
67
59
|
super.viewDidLoad()
|
@@ -72,169 +64,229 @@
|
|
72
64
|
|
73
65
|
sceneView.autoenablesDefaultLighting = true
|
74
66
|
|
67
|
+
|
68
|
+
|
69
|
+
//let configuration = ARWorldTrackingConfiguration()
|
70
|
+
|
71
|
+
//sceneView.session.run(configuration)
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
//override func didReceiveMemoryWarning() {
|
80
|
+
|
81
|
+
//super.didReceiveMemoryWarning()
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
override func viewWillAppear(_ animated: Bool) {
|
88
|
+
|
89
|
+
super.viewWillAppear(animated)
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
sceneView.session.run(defaultConfigulation)
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
override func viewWillDisappear(_ animated: Bool) {
|
102
|
+
|
103
|
+
super.viewWillDisappear(animated)
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
sceneView.session.pause()
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
//ボタンをタップしたら→showUIImagePicker
|
114
|
+
|
115
|
+
@IBAction func photoButtonTapped(_ sender: Any) {
|
116
|
+
|
117
|
+
showUIImagePicker()
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
//カメラロールから画像を取得する
|
124
|
+
|
125
|
+
private func showUIImagePicker() {
|
126
|
+
|
127
|
+
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
|
128
|
+
|
129
|
+
let pickerView = UIImagePickerController()
|
130
|
+
|
131
|
+
pickerView.sourceType = .photoLibrary
|
132
|
+
|
133
|
+
pickerView.delegate = self
|
134
|
+
|
135
|
+
pickerView.modalPresentationStyle = .overFullScreen
|
136
|
+
|
137
|
+
self.present(pickerView, animated: true, completion: nil)
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
//UIImageをsceneViewに貼る
|
146
|
+
|
147
|
+
//createPhotoNodeで作ったSCNNodeをsceneViewに追加
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
private func setImageToScene(image: UIImage) {
|
152
|
+
|
153
|
+
if let camera = sceneView.pointOfView {
|
154
|
+
|
155
|
+
let position = SCNVector3(x: 0, y: 0, z: -0.5)
|
156
|
+
|
157
|
+
let convertPosition = camera.convertPosition(position, to: nil)
|
158
|
+
|
75
|
-
|
159
|
+
let node = createPhotoNode(image, position: convertPosition)
|
160
|
+
|
76
|
-
|
161
|
+
self.sceneView.scene.rootNode.addChildNode(node)
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
162
|
+
|
163
|
+
|
164
|
+
|
81
|
-
let pinch = UIPinchGestureRecognizer(
|
165
|
+
let pinch = UIPinchGestureRecognizer(
|
82
|
-
|
166
|
+
|
83
|
-
target:
|
167
|
+
target: node, //ここがおかしいと思われます。。。
|
84
|
-
|
168
|
+
|
85
|
-
action: #selector(type(of: self).scenePinchGesture(_:))
|
169
|
+
action: #selector(type(of: self).scenePinchGesture(_:))
|
170
|
+
|
171
|
+
)
|
172
|
+
|
173
|
+
pinch.delegate = self
|
174
|
+
|
175
|
+
sceneView.addGestureRecognizer(pinch)
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
//SCNNodeを作る
|
186
|
+
|
187
|
+
private func createPhotoNode(_ image: UIImage, position: SCNVector3) -> SCNNode {
|
188
|
+
|
189
|
+
let node = SCNNode()
|
190
|
+
|
191
|
+
let scale: CGFloat = 0.3
|
192
|
+
|
193
|
+
let geometry = SCNBox(width: image.size.width * scale / image.size.height,
|
194
|
+
|
195
|
+
height: scale,
|
196
|
+
|
197
|
+
length: 0.00000001,
|
198
|
+
|
199
|
+
chamferRadius: 0.0)
|
200
|
+
|
201
|
+
geometry.firstMaterial?.diffuse.contents = image
|
202
|
+
|
203
|
+
node.geometry = geometry
|
204
|
+
|
205
|
+
node.position = position
|
206
|
+
|
207
|
+
return node
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
//ジェスチャーで呼ばれるメソッドを定義
|
214
|
+
|
215
|
+
@objc func scenePinchGesture(_ recognizer: UIPinchGestureRecognizer) {
|
216
|
+
|
217
|
+
if recognizer.state == .began {
|
218
|
+
|
219
|
+
lastGestureScale = 1
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
let newGestureScale: Float = Float(recognizer.scale)
|
226
|
+
|
227
|
+
let diff = newGestureScale - lastGestureScale
|
228
|
+
|
229
|
+
let createPhotoNode = SCNNode()
|
230
|
+
|
231
|
+
let currentScale = createPhotoNode.scale
|
232
|
+
|
233
|
+
createPhotoNode.scale = SCNVector3Make(
|
234
|
+
|
235
|
+
currentScale.x * (1 + diff),
|
236
|
+
|
237
|
+
currentScale.y * (1 + diff),
|
238
|
+
|
239
|
+
currentScale.z * (1 + diff)
|
86
240
|
|
87
241
|
)
|
88
242
|
|
89
|
-
pinch.delegate = self
|
90
|
-
|
91
|
-
scene
|
243
|
+
lastGestureScale = newGestureScale
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
244
|
+
|
96
|
-
|
97
|
-
sceneView.session.run(configuration)
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
}
|
245
|
+
}
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
246
|
+
|
106
|
-
|
107
|
-
super.didReceiveMemoryWarning()
|
108
|
-
|
109
|
-
|
247
|
+
}
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
|
252
|
+
|
110
|
-
|
253
|
+
extension GestureViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate,
|
254
|
+
|
111
|
-
|
255
|
+
UIGestureRecognizerDelegate{
|
256
|
+
|
257
|
+
|
258
|
+
|
112
|
-
|
259
|
+
//setImageToSeneにshowUIImagePickerで取得したカメラロールのUIimageを入れる
|
260
|
+
|
261
|
+
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info :[UIImagePickerController.InfoKey : Any]) {
|
262
|
+
|
113
|
-
|
263
|
+
if let image = info[.originalImage] as? UIImage {
|
114
|
-
|
264
|
+
|
115
|
-
s
|
265
|
+
setImageToScene(image: image)
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
sceneView.session.run(defaultConfigulation)
|
120
|
-
|
121
|
-
}
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
override func viewWillDisappear(_ animated: Bool) {
|
126
|
-
|
127
|
-
super.viewWillDisappear(animated)
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
sceneView.session.pause()
|
132
|
-
|
133
|
-
}
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
@IBAction func photoButtonTapped(_ sender: Any) {
|
138
|
-
|
139
|
-
showUIImagePicker()
|
140
|
-
|
141
|
-
}
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
private func showUIImagePicker() {
|
146
|
-
|
147
|
-
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
|
148
|
-
|
149
|
-
let pickerView = UIImagePickerController()
|
150
|
-
|
151
|
-
pickerView.sourceType = .photoLibrary
|
152
|
-
|
153
|
-
pickerView.delegate = self
|
154
|
-
|
155
|
-
pickerView.modalPresentationStyle = .overFullScreen
|
156
|
-
|
157
|
-
self.present(pickerView, animated: true, completion: nil)
|
158
266
|
|
159
267
|
}
|
160
268
|
|
269
|
+
picker.dismiss(animated: true, completion: nil)
|
270
|
+
|
161
|
-
}
|
271
|
+
}
|
162
|
-
|
163
|
-
|
164
|
-
|
272
|
+
|
273
|
+
|
274
|
+
|
165
|
-
|
275
|
+
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
|
166
|
-
|
167
|
-
|
276
|
+
|
168
|
-
|
169
|
-
let position = SCNVector3(x: 0, y: 0, z: -0.5)
|
170
|
-
|
171
|
-
|
277
|
+
picker.dismiss(animated: true, completion: nil)
|
172
|
-
|
173
|
-
|
278
|
+
|
174
|
-
|
175
|
-
self.sceneView.scene.rootNode.addChildNode(node)//ここでCannot convert value of type '(UIImage, SCNVector3) -> SCNNode' to expected argument type 'SCNNode'発生
|
176
|
-
|
177
|
-
|
279
|
+
}
|
178
|
-
|
179
|
-
|
280
|
+
|
180
|
-
|
181
|
-
|
182
|
-
|
281
|
+
|
282
|
+
|
283
|
+
|
284
|
+
|
183
|
-
private func
|
285
|
+
private func gestureRecognizer(_: UIPinchGestureRecognizer,
|
184
|
-
|
185
|
-
|
286
|
+
|
186
|
-
|
187
|
-
let scale: CGFloat = 0.3
|
188
|
-
|
189
|
-
le
|
287
|
+
shouldRecognizeSimultaneouslyWith shouldRecogunizeSimultaneouslyWithGestureRecognizer: UIGestureRecognizer) -> Bool{
|
190
|
-
|
191
|
-
|
288
|
+
|
192
|
-
|
193
|
-
length: 0.00000001,
|
194
|
-
|
195
|
-
chamferRadius: 0.0)
|
196
|
-
|
197
|
-
geometry.firstMaterial?.diffuse.contents = image
|
198
|
-
|
199
|
-
node.geometry = geometry
|
200
|
-
|
201
|
-
node.position = position
|
202
|
-
|
203
|
-
return
|
289
|
+
return true
|
204
|
-
|
205
|
-
}
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
@objc func scenePinchGesture(_ recognizer: UIPinchGestureRecognizer) {
|
210
|
-
|
211
|
-
if recognizer.state == .began {
|
212
|
-
|
213
|
-
lastGestureScale = 1
|
214
|
-
|
215
|
-
}
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
let newGestureScale: Float = Float(recognizer.scale)
|
220
|
-
|
221
|
-
let diff = newGestureScale - lastGestureScale
|
222
|
-
|
223
|
-
let currentScale = createPhotoNode.scale
|
224
|
-
|
225
|
-
//ここでValue of type '(UIImage, SCNVector3) -> SCNNode' has no member 'scale'のエラー発生
|
226
|
-
|
227
|
-
createPhotoNode.scale = SCNVector3Make(
|
228
|
-
|
229
|
-
currentScale.x * (1 + diff),
|
230
|
-
|
231
|
-
currentScale.y * (1 + diff),
|
232
|
-
|
233
|
-
currentScale.z * (1 + diff)
|
234
|
-
|
235
|
-
)
|
236
|
-
|
237
|
-
lastGestureScale = newGestureScale
|
238
290
|
|
239
291
|
}
|
240
292
|
|
@@ -242,46 +294,6 @@
|
|
242
294
|
|
243
295
|
|
244
296
|
|
245
|
-
extension GestureViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate,
|
246
|
-
|
247
|
-
UIGestureRecognizerDelegate{
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info :[UIImagePickerController.InfoKey : Any]) {
|
252
|
-
|
253
|
-
if let image = info[.originalImage] as? UIImage {
|
254
|
-
|
255
|
-
setImageToScene(image: image)
|
256
|
-
|
257
|
-
}
|
258
|
-
|
259
|
-
picker.dismiss(animated: true, completion: nil)
|
260
|
-
|
261
|
-
}
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
|
266
|
-
|
267
|
-
picker.dismiss(animated: true, completion: nil)
|
268
|
-
|
269
|
-
}
|
270
|
-
|
271
|
-
private func gestureRecognizer(_: UIPinchGestureRecognizer,
|
272
|
-
|
273
|
-
shouldRecognizeSimultaneouslyWith shouldRecogunizeSimultaneouslyWithGestureRecognizer: UIGestureRecognizer) -> Bool{
|
274
|
-
|
275
|
-
return true
|
276
|
-
|
277
|
-
}
|
278
|
-
|
279
|
-
}
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
297
|
```
|
286
298
|
|
287
299
|
|
@@ -290,7 +302,7 @@
|
|
290
302
|
|
291
303
|
|
292
304
|
|
293
|
-
|
305
|
+
setImageToSceneのインスタンスをUIPinchGestureRecognizerのターゲットに指定しようとしましたができませんでした。
|
294
306
|
|
295
307
|
|
296
308
|
|