質問編集履歴

2

誤字

2019/11/07 16:47

投稿

shiosako
shiosako

スコア6

test CHANGED
File without changes
test CHANGED
@@ -14,7 +14,9 @@
14
14
 
15
15
  ViewController
16
16
 
17
+
18
+
17
- ...swift
19
+ ```swift
18
20
 
19
21
  //画像send
20
22
 
@@ -100,13 +102,13 @@
100
102
 
101
103
  }
102
104
 
103
- ...
105
+ ```
104
106
 
105
107
 
106
108
 
107
109
  ViewController2
108
110
 
109
- ...
111
+ ```
110
112
 
111
113
  @IBOutlet weak var cameraView: UIImageView!
112
114
 
@@ -154,4 +156,4 @@
154
156
 
155
157
  }
156
158
 
157
- ...
159
+ ```

1

誤字

2019/11/07 16:47

投稿

shiosako
shiosako

スコア6

test CHANGED
File without changes
test CHANGED
@@ -7,3 +7,151 @@
7
7
  ですが、画像が渡せていないのか、スクリーンショットがうまくできていないのか
8
8
 
9
9
  遷移先のViewControllerで画像を表示させることができません。
10
+
11
+ エラーは出ていません。
12
+
13
+
14
+
15
+ ViewController
16
+
17
+ ...swift
18
+
19
+ //画像send
20
+
21
+ let defaults = UserDefaults.standard
22
+
23
+ var capturedImage: UIImage!
24
+
25
+
26
+
27
+ //画像キャプチャー
28
+
29
+ func getScreenShot(windowFrame: CGRect) -> UIImage {
30
+
31
+
32
+
33
+ UIGraphicsBeginImageContextWithOptions(windowFrame.size, false, 0.0)
34
+
35
+
36
+
37
+ self.view.drawHierarchy(in: self.view.bounds, afterScreenUpdates: true)
38
+
39
+ //let context: CGContext = UIGraphicsGetCurrentContext()!
40
+
41
+ //self.view.layer.render(in: context)
42
+
43
+ capturedImage = UIGraphicsGetImageFromCurrentImageContext()!
44
+
45
+ UIGraphicsEndImageContext()
46
+
47
+
48
+
49
+ return capturedImage
50
+
51
+ }
52
+
53
+ //savebutton
54
+
55
+ @IBAction func saveButton(_ sender: Any) {
56
+
57
+ capturedImage = getScreenShot(windowFrame: self.cameraView.bounds)
58
+
59
+ defaults.UIImageToData(image: capturedImage, forKey: "image")
60
+
61
+ // sendSaveImage()
62
+
63
+ }
64
+
65
+ }
66
+
67
+ extension UserDefaults {
68
+
69
+ // 保存したいUIImage, 保存するUserDefaults, Keyを取得
70
+
71
+ func UIImageToData(image: UIImage, forKey: String) {
72
+
73
+ // UIImageをData型へ変換
74
+
75
+ let nsdata = image.pngData()
76
+
77
+ // UserDefaultsへ保存
78
+
79
+ self.set(nsdata, forKey: forKey)
80
+
81
+ }
82
+
83
+ // 参照するUserDefaults, Keyを取得, UIImageを返す
84
+
85
+ func getImageData(forKey: String) -> UIImage {
86
+
87
+ // UserDefaultsからKeyを基にData型を参照
88
+
89
+ let data = self.data(forKey: forKey)
90
+
91
+ // UIImage型へ変換
92
+
93
+ let returnImage = UIImage(data: data!)
94
+
95
+ // UIImageを返す
96
+
97
+ return returnImage!
98
+
99
+ }
100
+
101
+ }
102
+
103
+ ...
104
+
105
+
106
+
107
+ ViewController2
108
+
109
+ ...
110
+
111
+ @IBOutlet weak var cameraView: UIImageView!
112
+
113
+ //画像取得
114
+
115
+ let defaults = UserDefaults.standard
116
+
117
+ override func viewDidLoad() {
118
+
119
+ super.viewDidLoad()
120
+
121
+ //
122
+
123
+
124
+
125
+ self.view.backgroundColor = UIColor.init(red: 230/255, green: 255/255, blue: 230/255, alpha: 90/100)
126
+
127
+ capturedImage = defaults.getImageData(forKey: "image")
128
+
129
+
130
+
131
+ let rect = cameraView.frame
132
+
133
+ imageView = UIImageView(frame:rect)
134
+
135
+ imageView.contentMode = .scaleAspectFit
136
+
137
+ imageView.image = capturedImage
138
+
139
+ imageView.center = self.cameraView.center
140
+
141
+ self.cameraView.addSubview(imageView)
142
+
143
+
144
+
145
+ }
146
+
147
+ @IBAction func saveButton(_ sender: Any) {
148
+
149
+ let image = mergeImage(windowFrame: cameraView.frame)
150
+
151
+ UIImageWriteToSavedPhotosAlbum(image,self,#selector(self.didFinishSavingImage(_:didFinishSavingWithError:contextInfo:)),nil)
152
+
153
+ }
154
+
155
+ }
156
+
157
+ ...