質問編集履歴

1

ソースコードの追加

2015/12/10 06:44

投稿

hatta-
hatta-

スコア8

test CHANGED
@@ -1 +1 @@
1
- コアデータに保存したスクショ画像を別の画面で表示させ方法
1
+ コアデータに保存したスクショ画像を別の画面で表示ことができなくて困っています。
test CHANGED
@@ -1,7 +1,279 @@
1
1
  [プログラミング初心者、swift歴3ヶ月]
2
2
 
3
+ ・前提・実現したいこと
4
+
5
+ --------------------------------------------------------------------------------------
6
+
7
+ 画面をスクリーンショットし、それをコアデータに保存。画面推移をした画面でコアデータに保存したスクショ画像を表示させる機能を実装したいです。
8
+
9
+
10
+
11
+ ・発生している問題
12
+
13
+ ------------------------------------------------------------------------------------
14
+
3
- 画面をスクリーンショットし、それをコアデータに保存。面推移をした画面でコアデータに保存したスクショ画を表示させる機能を実装したいです。しかし現在、スクショ画像の表示ができなくて困っています。調べても保存した文字列を表示させる方法が出るだけで、画像を表示させる方法がわからりません。保存まではできています。
15
+ スクショ画像の表示ができなくて困っています。調べても保存した文字列を表示させる方法が出るだけで、画像を表示させる方法がわからりません。保存まではできています。
16
+
17
+ 以前回答して頂いた通りにやってみてもエラーがでて、そこから自分なりに改善してみた結果も'[(Picture)]' does not have a member named 'picture'というエラーがでています。
4
18
 
5
19
  コアデータに保存した画像を表示させる方法を教えていただきたいです。
6
20
 
7
21
  よろしくお願いいたします。
22
+
23
+
24
+
25
+ ・ソースコード
26
+
27
+ --------------------------------------------------------------------------------------
28
+
29
+ ShowViewController.swift (スクショされた画像を表示する。問題がおきている方です。)
30
+
31
+ ```swift
32
+
33
+ import UIKit
34
+
35
+ import CoreData
36
+
37
+
38
+
39
+ class ShowViewController: UIViewController {
40
+
41
+
42
+
43
+
44
+
45
+ var myPicture = [Picture]()
46
+
47
+
48
+
49
+
50
+
51
+ override func viewDidLoad() {
52
+
53
+ super.viewDidLoad()
54
+
55
+ readData()
56
+
57
+
58
+
59
+ }
60
+
61
+
62
+
63
+ override func didReceiveMemoryWarning() {
64
+
65
+ super.didReceiveMemoryWarning()
66
+
67
+ }
68
+
69
+
70
+
71
+ func readData(){
72
+
73
+
74
+
75
+ let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
76
+
77
+ let myContext: NSManagedObjectContext = appDel.managedObjectContext!
78
+
79
+
80
+
81
+ let myRequest: NSFetchRequest = NSFetchRequest(entityName: "Picture")
82
+
83
+ myRequest.returnsObjectsAsFaults = false
84
+
85
+
86
+
87
+ //var results: NSArray! = myContext.executeFetchRequest(myRequest, error: nil)
88
+
89
+
90
+
91
+ myPicture = myContext.executeFetchRequest(myRequest, error: nil)as! [Picture]
92
+
93
+
94
+
95
+ // UIImage インスタンスの生成
96
+
97
+ let image1:UIImage? = UIImage(data: myPicture.picture) **エラー部分'[(Picture)]' does not have a member named 'picture'
98
+
99
+
100
+
101
+ // UIImageView 初期化
102
+
103
+ let imageView = UIImageView(image:image1)
104
+
105
+
106
+
107
+ // 画像の中心を187.5, 333.5 の位置に設定、iPhone6
108
+
109
+ imageView.center = CGPointMake(187.5, 333.5)
110
+
111
+
112
+
113
+ // UIImageViewのインスタンスをビューに追加
114
+
115
+ self.view.addSubview(imageView)
116
+
117
+
118
+
119
+ }
120
+
121
+ }
122
+
123
+
124
+
125
+ ```
126
+
127
+
128
+
129
+
130
+
131
+
132
+
133
+
134
+
135
+ Viewcontroller.swift (写真が表示されていて、それをスクショする。)
136
+
137
+ ```swift
138
+
139
+ import UIKit
140
+
141
+ import CoreData
142
+
143
+
144
+
145
+ class ViewController: UIViewController {
146
+
147
+
148
+
149
+ @IBOutlet weak var screenShotImageView: UIImageView!
150
+
151
+
152
+
153
+
154
+
155
+ override func viewDidLoad() {
156
+
157
+ super.viewDidLoad()
158
+
159
+ // Do any additional setup after loading the view, typically from a nib.
160
+
161
+ }
162
+
163
+
164
+
165
+ override func didReceiveMemoryWarning() {
166
+
167
+ super.didReceiveMemoryWarning()
168
+
169
+ // Dispose of any resources that can be recreated.
170
+
171
+ }
172
+
173
+
174
+
175
+ func screenShot() {
176
+
177
+ // スクリーンショットの取得開始
178
+
179
+ UIGraphicsBeginImageContextWithOptions(view.bounds.size, true, 1.0)
180
+
181
+
182
+
183
+ // 描画
184
+
185
+ view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
186
+
187
+
188
+
189
+ // 描画が行われたスクリーンショットの取得
190
+
191
+ let screenShot = UIGraphicsGetImageFromCurrentImageContext()
192
+
193
+
194
+
195
+ // スクリーンショットの取得終了
196
+
197
+ UIGraphicsEndImageContext()
198
+
199
+
200
+
201
+ // 取得したスクリーンショットを表示
202
+
203
+ //screenShotImageView.image = screenShot
204
+
205
+
206
+
207
+ //取得したスクリーンショットをコアデータに入れる
208
+
209
+ var imageData = UIImagePNGRepresentation(screenShot)
210
+
211
+
212
+
213
+ let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
214
+
215
+ let myContext: NSManagedObjectContext = appDel.managedObjectContext!
216
+
217
+
218
+
219
+ let myEntity: NSEntityDescription! = NSEntityDescription.entityForName("Picture", inManagedObjectContext: myContext)
220
+
221
+
222
+
223
+ var newData = Picture(entity: myEntity, insertIntoManagedObjectContext: myContext)
224
+
225
+ newData.picture = imageData
226
+
227
+
228
+
229
+ }
230
+
231
+
232
+
233
+
234
+
235
+ @IBAction func screenShot(sender: AnyObject) {
236
+
237
+ screenShot()
238
+
239
+ }
240
+
241
+ }
242
+
243
+ ```
244
+
245
+
246
+
247
+ Picture.swift (コアデータ)
248
+
249
+ ```swift
250
+
251
+ import UIKit
252
+
253
+ import Foundation
254
+
255
+ import CoreData
256
+
257
+
258
+
259
+ class Picture: NSManagedObject {
260
+
261
+
262
+
263
+ @NSManaged var picture: NSData
264
+
265
+
266
+
267
+ }
268
+
269
+
270
+
271
+ ```
272
+
273
+
274
+
275
+ コアデータの内容
276
+
277
+ ENTITIES : Picture
278
+
279
+ Attributes : picture Type:Binary Data