質問編集履歴
1
修正
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
XcodeのThread 4: signal SIGABRTエラーについて
|
body
CHANGED
@@ -1,15 +1,132 @@
|
|
1
|
+
開発環境
|
1
|
-
|
2
|
+
Xcode 11.6
|
3
|
+
Swift 5.2.4
|
2
4
|
|
3
|
-
|
5
|
+
現在Xcodeで
|
4
|
-
|
6
|
+
「絶対に挫折しないiPhoneアプリ開発「超」入門」を読み進めながらカメラアプリを作成しています
|
5
7
|
|
8
|
+
実機(iPhone)で動かして見ると
|
9
|
+
UsePhotoと言うボタンを押すと
|
10
|
+
```Swift
|
11
|
+
Thread 4: signal SIGABRT //4のところの数字は毎回変わります
|
12
|
+
```
|
6
|
-
|
13
|
+
と言うエラーが出て止まってしまいます
|
7
|
-
Viewを追加して[こちらの記事](https://swallow-incubate.com/archives/blog/20200226/)を参考にしながらページ繊維を実装したところ
|
8
14
|
|
9
|
-
ページ遷移を実装したボタンだけ表示されなくなってしまいました
|
10
|
-
※画像左下の鉛筆型のボタンです
|
11
|
-
|
15
|
+
Storyboardなら[この記事](https://qiita.com/nekonekonekoe/items/ebef3446bca42e8babfd)に書いてある方法で関連付けを切れば良いとわかったのですがSwiftUIの場合どうしたら良いかわかりません
|
12
16
|
|
13
|
-
|
17
|
+
ソースコード
|
14
18
|
|
19
|
+
ContentView.swift
|
20
|
+
```Swift
|
21
|
+
import SwiftUI
|
22
|
+
|
23
|
+
struct ContentView: View {
|
24
|
+
@State var image:Image?
|
25
|
+
@State var isPicking = false
|
26
|
+
var body: some View {
|
27
|
+
ZStack {
|
28
|
+
VStack {
|
29
|
+
VStack {
|
30
|
+
Spacer()
|
31
|
+
image?
|
32
|
+
.resizable()
|
33
|
+
.scaledToFit()
|
34
|
+
Spacer()
|
35
|
+
}
|
36
|
+
HStack {
|
37
|
+
Spacer()
|
15
|
-
|
38
|
+
Button(action: {
|
39
|
+
self.isPicking = true
|
40
|
+
}) {
|
41
|
+
Image(systemName: "camera")
|
42
|
+
Text("カメラ")
|
43
|
+
}.padding()
|
44
|
+
}
|
45
|
+
}
|
46
|
+
if isPicking {
|
47
|
+
ImagePicker(image: $image, isPicking: $isPicking)
|
48
|
+
.edgesIgnoringSafeArea(.all)
|
49
|
+
.transition(.move(edge: .bottom))
|
50
|
+
.animation(.easeInOut)
|
51
|
+
}
|
52
|
+
}
|
53
|
+
}
|
54
|
+
}
|
55
|
+
|
56
|
+
```
|
57
|
+
|
58
|
+
Coordinator.swift
|
59
|
+
```Swift
|
60
|
+
import SwiftUI
|
61
|
+
|
62
|
+
class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
|
63
|
+
|
64
|
+
var parent:ImagePicker
|
65
|
+
|
66
|
+
init(_ parent:ImagePicker) {
|
67
|
+
self.parent = parent
|
68
|
+
}
|
69
|
+
|
70
|
+
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
|
71
|
+
let uiImage = info[.originalImage] as! UIImage
|
72
|
+
UIImageWriteToSavedPhotosAlbum(uiImage, nil, nil, nil)
|
73
|
+
parent.image = Image(uiImage: uiImage.redraw())
|
74
|
+
parent.isPicking = false
|
75
|
+
}
|
76
|
+
|
77
|
+
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
|
78
|
+
parent.isPicking = false
|
79
|
+
}
|
80
|
+
}
|
81
|
+
|
82
|
+
```
|
83
|
+
|
84
|
+
ImagePicker.swift
|
85
|
+
```Swift
|
86
|
+
import SwiftUI
|
87
|
+
|
88
|
+
struct ImagePicker: UIViewControllerRepresentable {
|
89
|
+
|
90
|
+
@Binding var image:Image?
|
91
|
+
@Binding var isPicking:Bool
|
92
|
+
|
93
|
+
func makeCoordinator() -> Coordinator {
|
94
|
+
Coordinator(self)
|
95
|
+
}
|
96
|
+
|
97
|
+
func makeUIViewController(context: Context) -> UIImagePickerController {
|
98
|
+
let picker = UIImagePickerController()
|
99
|
+
picker.sourceType = .camera
|
100
|
+
picker.delegate = context.coordinator
|
101
|
+
return picker
|
102
|
+
}
|
103
|
+
|
104
|
+
func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {
|
105
|
+
|
106
|
+
}
|
107
|
+
}
|
108
|
+
|
109
|
+
struct ImagePicker_Previews: PreviewProvider {
|
110
|
+
static var previews: some View {
|
111
|
+
ImagePicker(image: .constant(nil), isPicking: .constant(true))
|
112
|
+
}
|
113
|
+
}
|
114
|
+
|
115
|
+
```
|
116
|
+
|
117
|
+
UIImageExtensions.swift
|
118
|
+
```Swift
|
119
|
+
import UIKit
|
120
|
+
|
121
|
+
extension UIImage {
|
122
|
+
func redraw() -> UIImage {
|
123
|
+
let format = UIGraphicsImageRendererFormat()
|
124
|
+
format.scale = 1
|
125
|
+
return UIGraphicsImageRenderer(size: size, format: format)
|
126
|
+
.image {
|
127
|
+
context in
|
128
|
+
draw(in: CGRect(origin: .zero, size: size))
|
129
|
+
}
|
130
|
+
}
|
131
|
+
}
|
132
|
+
```
|