質問編集履歴
1
修正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
XcodeのThread 4: signal SIGABRTエラーについて
|
test
CHANGED
@@ -1,29 +1,263 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
1
|
+
開発環境
|
2
|
+
|
3
|
+
Xcode 11.6
|
4
|
+
|
5
|
+
Swift 5.2.4
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
現在Xcodeで
|
10
|
+
|
11
|
+
「絶対に挫折しないiPhoneアプリ開発「超」入門」を読み進めながらカメラアプリを作成しています
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
実機(iPhone)で動かして見ると
|
16
|
+
|
17
|
+
UsePhotoと言うボタンを押すと
|
18
|
+
|
19
|
+
```Swift
|
20
|
+
|
21
|
+
Thread 4: signal SIGABRT //4のところの数字は毎回変わります
|
22
|
+
|
23
|
+
```
|
24
|
+
|
25
|
+
と言うエラーが出て止まってしまいます
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
Storyboardなら[この記事](https://qiita.com/nekonekonekoe/items/ebef3446bca42e8babfd)に書いてある方法で関連付けを切れば良いとわかったのですがSwiftUIの場合どうしたら良いかわかりません
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
ソースコード
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
ContentView.swift
|
38
|
+
|
39
|
+
```Swift
|
40
|
+
|
41
|
+
import SwiftUI
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
struct ContentView: View {
|
46
|
+
|
47
|
+
@State var image:Image?
|
48
|
+
|
49
|
+
@State var isPicking = false
|
50
|
+
|
51
|
+
var body: some View {
|
52
|
+
|
53
|
+
ZStack {
|
54
|
+
|
55
|
+
VStack {
|
56
|
+
|
57
|
+
VStack {
|
58
|
+
|
59
|
+
Spacer()
|
60
|
+
|
61
|
+
image?
|
62
|
+
|
63
|
+
.resizable()
|
64
|
+
|
65
|
+
.scaledToFit()
|
66
|
+
|
67
|
+
Spacer()
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
HStack {
|
72
|
+
|
73
|
+
Spacer()
|
74
|
+
|
75
|
+
Button(action: {
|
76
|
+
|
77
|
+
self.isPicking = true
|
78
|
+
|
79
|
+
}) {
|
80
|
+
|
81
|
+
Image(systemName: "camera")
|
82
|
+
|
83
|
+
Text("カメラ")
|
84
|
+
|
85
|
+
}.padding()
|
86
|
+
|
87
|
+
}
|
88
|
+
|
89
|
+
}
|
90
|
+
|
91
|
+
if isPicking {
|
92
|
+
|
93
|
+
ImagePicker(image: $image, isPicking: $isPicking)
|
94
|
+
|
95
|
+
.edgesIgnoringSafeArea(.all)
|
96
|
+
|
97
|
+
.transition(.move(edge: .bottom))
|
98
|
+
|
99
|
+
.animation(.easeInOut)
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
```
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
Coordinator.swift
|
116
|
+
|
117
|
+
```Swift
|
118
|
+
|
119
|
+
import SwiftUI
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
var parent:ImagePicker
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
init(_ parent:ImagePicker) {
|
132
|
+
|
133
|
+
self.parent = parent
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
|
140
|
+
|
141
|
+
let uiImage = info[.originalImage] as! UIImage
|
142
|
+
|
143
|
+
UIImageWriteToSavedPhotosAlbum(uiImage, nil, nil, nil)
|
144
|
+
|
145
|
+
parent.image = Image(uiImage: uiImage.redraw())
|
146
|
+
|
147
|
+
parent.isPicking = false
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
|
154
|
+
|
155
|
+
parent.isPicking = false
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
```
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
ImagePicker.swift
|
168
|
+
|
169
|
+
```Swift
|
170
|
+
|
171
|
+
import SwiftUI
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
struct ImagePicker: UIViewControllerRepresentable {
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
@Binding var image:Image?
|
180
|
+
|
181
|
+
@Binding var isPicking:Bool
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
func makeCoordinator() -> Coordinator {
|
186
|
+
|
187
|
+
Coordinator(self)
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
func makeUIViewController(context: Context) -> UIImagePickerController {
|
194
|
+
|
195
|
+
let picker = UIImagePickerController()
|
196
|
+
|
197
|
+
picker.sourceType = .camera
|
198
|
+
|
199
|
+
picker.delegate = context.coordinator
|
200
|
+
|
201
|
+
return picker
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
struct ImagePicker_Previews: PreviewProvider {
|
218
|
+
|
219
|
+
static var previews: some View {
|
220
|
+
|
221
|
+
ImagePicker(image: .constant(nil), isPicking: .constant(true))
|
222
|
+
|
223
|
+
}
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
```
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
UIImageExtensions.swift
|
234
|
+
|
235
|
+
```Swift
|
236
|
+
|
237
|
+
import UIKit
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
extension UIImage {
|
242
|
+
|
243
|
+
func redraw() -> UIImage {
|
244
|
+
|
245
|
+
let format = UIGraphicsImageRendererFormat()
|
246
|
+
|
247
|
+
format.scale = 1
|
248
|
+
|
249
|
+
return UIGraphicsImageRenderer(size: size, format: format)
|
250
|
+
|
251
|
+
.image {
|
252
|
+
|
253
|
+
context in
|
254
|
+
|
255
|
+
draw(in: CGRect(origin: .zero, size: size))
|
256
|
+
|
257
|
+
}
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
```
|