質問編集履歴

2

最終的なコードの記載

2019/10/28 17:36

投稿

Jonny_dayo
Jonny_dayo

スコア48

test CHANGED
File without changes
test CHANGED
@@ -187,3 +187,99 @@
187
187
  Xcode : Version 11.0
188
188
 
189
189
  Swift : Apple Swift version 5.1
190
+
191
+
192
+
193
+ ### 最終的なコード
194
+
195
+ ```ここに言語を入力
196
+
197
+ var imgArray:[UIImage] = []
198
+
199
+ var img: [String] = ["ぞう","うさぎ","あるぱか","ぶた","はむ","ひよこ","いぬ","くま","はち","ねこ","ぱんだ","ぺんぎん"]
200
+
201
+ var img01 :UIImage = UIImage(named:"ぞう")!
202
+
203
+ var img02 :UIImage = UIImage(named:"うさぎ")!
204
+
205
+ var img03 :UIImage = UIImage(named:"あるぱか")!
206
+
207
+ var img04 :UIImage = UIImage(named:"ぶた")!
208
+
209
+ var img05 :UIImage = UIImage(named:"はむ")!
210
+
211
+ var img06 :UIImage = UIImage(named:"ひよこ")!
212
+
213
+ var img07 :UIImage = UIImage(named:"いぬ")!
214
+
215
+ var img08 :UIImage = UIImage(named:"くま")!
216
+
217
+ var img09 :UIImage = UIImage(named:"はち")!
218
+
219
+ var img10 :UIImage = UIImage(named:"ねこ")!
220
+
221
+ var img11 :UIImage = UIImage(named:"ぱんだ")!
222
+
223
+ var img12 :UIImage = UIImage(named:"ぺんぎん")!
224
+
225
+ let udf = UserDefaults.standard
226
+
227
+ var imageName = ""
228
+
229
+
230
+
231
+ override func viewDidLoad() {
232
+
233
+ super.viewDidLoad()
234
+
235
+
236
+
237
+ imageset()
238
+
239
+ }
240
+
241
+ private func imageset(){
242
+
243
+
244
+
245
+ let iv = UIImageView(frame: CGRect(x: 20, y: 90, width: 60, height: 60))
246
+
247
+ let ret = Int(arc4random_uniform(12))
248
+
249
+ imageName = img[ret]
250
+
251
+ iv.image = UIImage(named: imageName)
252
+
253
+ iv.backgroundColor = UIColor.systemBlue
254
+
255
+ iv.layer.cornerRadius = iv.frame.height
256
+
257
+ iv.clipsToBounds = true
258
+
259
+ self.view.addSubview(iv)
260
+
261
+
262
+
263
+ }
264
+
265
+ @IBAction func save(_ sender: Any) {
266
+
267
+
268
+
269
+ var imagedata: [String] = udf.array(forKey: "imageName") as? [String] ?? []
270
+
271
+ imagedata.append(imageName)
272
+
273
+ udf.set(imagedata, forKey: "imageName")
274
+
275
+ self.dismiss(animated: true, completion: nil)
276
+
277
+
278
+
279
+ }
280
+
281
+ }
282
+
283
+
284
+
285
+ ```

1

回答を反映

2019/10/28 17:36

投稿

Jonny_dayo
Jonny_dayo

スコア48

test CHANGED
File without changes
test CHANGED
@@ -18,31 +18,21 @@
18
18
 
19
19
  ```
20
20
 
21
- var imgArray:[UIImage] = []
21
+ var imageArray: [UIImage] = [
22
22
 
23
- var img01 :UIImage = UIImage(named:"う")!
23
+ UIImage(named: "ぞう.jpeg")!, UIImage(named: "うさぎ.jpeg")!,
24
24
 
25
- var img02 :UIImage = UIImage(named:"うさぎ")!
25
+ UIImage(named: "あるぱか.jpeg")!, UIImage(named: "ぶた.jpeg")!,
26
26
 
27
- var img03 :UIImage = UIImage(named:"あるぱか")!
27
+ UIImage(named: "はむ.jpeg")!, UIImage(named: "ひよこ.jpeg")!,
28
28
 
29
- var img04 :UIImage = UIImage(named:"ぶた")!
29
+ UIImage(named: "いぬ.jpeg")!, UIImage(named: "くま.jpeg")!,
30
30
 
31
- var img05 :UIImage = UIImage(named:"はむ")!
31
+ UIImage(named: "はち.jpeg")!, UIImage(named: "ねこ.jpeg")!,
32
32
 
33
- var img06 :UIImage = UIImage(named:"ひよこ")!
33
+ UIImage(named: "ぱんだ.jpeg")!, UIImage(named: "ぺんぎん.jpeg")!,
34
34
 
35
- var img07 :UIImage = UIImage(named:"いぬ")!
35
+ ]
36
-
37
- var img08 :UIImage = UIImage(named:"くま")!
38
-
39
- var img09 :UIImage = UIImage(named:"はち")!
40
-
41
- var img10 :UIImage = UIImage(named:"ねこ")!
42
-
43
- var img11 :UIImage = UIImage(named:"ぱんだ")!
44
-
45
- var img12 :UIImage = UIImage(named:"ぺんぎん")!
46
36
 
47
37
  let udf = UserDefaults.standard
48
38
 
@@ -68,11 +58,11 @@
68
58
 
69
59
  let iv = UIImageView(frame: CGRect(x: 20, y: 90, width: 60, height: 60))
70
60
 
71
- imgArray = [img01,img02,img03,img04,img05,img06,img07,img08,img09,img10,img11,img12]
61
+ //imgArray = [img01,img02,img03,img04,img05,img06,img07,img08,img09,img10,img11,img12]
72
62
 
73
63
  let ret = Int(arc4random_uniform(12))
74
64
 
75
- iv.image = imgArray[ret]
65
+ //iv.image = imgArray[ret]
76
66
 
77
67
  iv.backgroundColor = UIColor.systemBlue
78
68