質問編集履歴

4

質問文に勘違いがありましたので修正しました。

2021/09/28 01:00

投稿

yoppen9
yoppen9

スコア0

test CHANGED
File without changes
test CHANGED
@@ -1,12 +1,12 @@
1
- ### 画像をData型以外の型で配列に入れたい
1
+ ### 加工した画像を配列に入れてcollectionViewに並べたい
2
-
3
-
4
-
5
-
6
-
2
+
3
+
4
+
5
+
6
+
7
- トリミング完了画像を配列に入れてcollectionViewにて配列内の画像を並べたいのですが、現在UIImageをpngDataにして配列に入れてます。ですがpngDataで入れてしまうと、元の画像とトリミング後の画像のpngData名が同じになっていしまいトリミング前の画像サイズでcollectionViewに表示されてしまいます。
7
+ トリミング完了画像を配列に入れてcollectionViewにて配列内の画像を並べたいのですが、現在UIImageをpngDataにして配列に入れてます。ですがcollectionViewに表示すると、トリミング前の画像が並んでしまいます。
8
-
8
+
9
- トリミング完了後の画像を配列入れる方法はありますか?
9
+ トリミング完了後の画像を上手くcollectionView表示する方法はありますか?
10
10
 
11
11
 
12
12
 

3

コード追加しました

2021/09/28 01:00

投稿

yoppen9
yoppen9

スコア0

test CHANGED
File without changes
test CHANGED
@@ -172,13 +172,7 @@
172
172
 
173
173
  image3.image = UIImage(data: model.FamilyPhotos[indexPath.row])
174
174
 
175
- // guard let uiImage = UIImage(data: model.FamilyPhotos[indexPath.row]), let cgImage = uiImage.cgImage else { return UICollectionViewCell() }
176
-
177
- // image3.image = UIImage(cgImage: cgImage, scale: 0, orientation: uiImage.imageOrientation)
178
-
179
- }
175
+ }
180
-
181
-
182
176
 
183
177
  nameCell.delegate = self
184
178
 

2

コード追加しました

2021/09/18 00:58

投稿

yoppen9
yoppen9

スコア0

test CHANGED
@@ -1 +1 @@
1
- 【swift】トリミング完了画像の配列への入れ方がわからない
1
+ 【swift】画像の配列への入れ方がわからない
test CHANGED
@@ -1,14 +1,20 @@
1
- ### トリミング完了後のサイズ保ったままcollectiionView表示させたい
1
+ ### 画像Data型以外の型で配列入れたい
2
-
3
-
4
-
5
-
6
-
2
+
3
+
4
+
5
+
6
+
7
- トリミング完了画像を配列に入れてcollectionViewにて配列内の画像を並べたいのですが、現在UIImageをpngDataにして配列に入れてます。ですがpngDataで入れてしまうと、元の画像とトリミング後の画像のpngData名が同じなためトリミング前の画像サイズでcollectionViewに表示されてしまいます。
7
+ トリミング完了画像を配列に入れてcollectionViewにて配列内の画像を並べたいのですが、現在UIImageをpngDataにして配列に入れてます。ですがpngDataで入れてしまうと、元の画像とトリミング後の画像のpngData名が同じっていしまいトリミング前の画像サイズでcollectionViewに表示されてしまいます。
8
-
8
+
9
- トリミング完了後のサイズ保ったまま配列に入れること可能なのでしょうか?
9
+ トリミング完了後の画像を配列に入れる方法ありますか?
10
+
11
+
12
+
10
-
13
+ 説明が上手く出来ず申し訳ありません。理解できないところは質問してくれると幸いです。
14
+
15
+
16
+
11
- たどのようにれば良いのでしょうか?
17
+ 宜しくお願い致します
12
18
 
13
19
 
14
20
 

1

コードを追加しました。

2021/09/18 00:54

投稿

yoppen9
yoppen9

スコア0

test CHANGED
File without changes
test CHANGED
@@ -18,37 +18,423 @@
18
18
 
19
19
  ```TitleListModel
20
20
 
21
+ import Foundation
22
+
23
+
24
+
21
- //配列
25
+ enum Section {
26
+
22
-
27
+ case familySection
28
+
29
+ }
30
+
31
+
32
+
33
+ class TitleListModel {
34
+
35
+ var sectionTitle: String = "家族"
36
+
37
+ var section = Section.familySection
38
+
39
+ let cardsList = ["家族"]
40
+
41
+
42
+
43
+ var FamilyName: [String] = []
44
+
45
+ var FamilyPhone: [String] = []
46
+
23
- var FamilyPhotos: [Data] = []
47
+ var FamilyPhotos: [Data] = []
48
+
49
+ }
24
50
 
25
51
  ```
26
52
 
27
53
  ```CardListViewController
28
54
 
55
+ import UIKit
56
+
57
+
58
+
59
+ class CardListViewController: UIViewController, UIGestureRecognizerDelegate {
60
+
61
+
62
+
63
+ @IBOutlet weak var collectionView: UICollectionView!
64
+
65
+ @IBOutlet weak var collectionViewFlowLayout: UICollectionViewFlowLayout!
66
+
67
+ @IBOutlet weak var addBarButtonItem: UIBarButtonItem!
68
+
69
+
70
+
71
+ var model: TitleListModel!
72
+
73
+
74
+
29
- //collectionViewに並べる
75
+ override func loadView() {
76
+
30
-
77
+ super.loadView()
78
+
79
+
80
+
81
+ switch model.section {
82
+
83
+
84
+
85
+ case .familySection:
86
+
87
+ if(UserDefaults.standard.array(forKey: "Family1") != nil) {
88
+
89
+ model.FamilyName = UserDefaults.standard.array(forKey: "Family1") as! [String]
90
+
91
+ }
92
+
93
+ if(UserDefaults.standard.array(forKey: "Family2") != nil) {
94
+
95
+ model.FamilyPhone = UserDefaults.standard.array(forKey: "Family2") as! [String]
96
+
97
+ }
98
+
99
+ if(UserDefaults.standard.array(forKey: "Family3") != nil) {
100
+
101
+ model.FamilyPhotos = UserDefaults.standard.array(forKey: "Family3") as! [Data]
102
+
103
+ }
104
+
105
+ }
106
+
107
+ }
108
+
109
+ override func viewDidLoad() {
110
+
111
+ super.viewDidLoad()
112
+
113
+
114
+
115
+ collectionView.delegate = self
116
+
117
+ collectionView.dataSource = self
118
+
119
+
120
+
121
+ let nib = UINib(nibName: "CollectionViewCell", bundle: .main)
122
+
123
+ collectionView.register(nib, forCellWithReuseIdentifier: "Cell1")
124
+
125
+ }
126
+
127
+ @IBAction func settingButton(_ sender: Any) {
128
+
129
+ let nextView2 = storyboard?.instantiateViewController(identifier: "next2") as! ChildSettingViewController
130
+
131
+ nextView2.modalPresentationStyle = .fullScreen
132
+
133
+ nextView2.presentationController?.delegate = self
134
+
135
+ nextView2.model = self.model
136
+
137
+ present(nextView2, animated: true, completion: nil)
138
+
139
+ }
140
+
141
+ }
142
+
143
+
144
+
145
+ extension CardListViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
146
+
147
+ func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
148
+
149
+ let nameCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell1", for: indexPath) as! CollectionViewCell
150
+
151
+ let label = nameCell.contentView.viewWithTag(1) as! UILabel
152
+
153
+ let label2 = nameCell.contentView.viewWithTag(2) as! UILabel
154
+
31
- let image3 = nameCell.contentView.viewWithTag(3) as! UIImageView
155
+ let image3 = nameCell.contentView.viewWithTag(3) as! UIImageView
156
+
157
+
158
+
32
-
159
+ switch model.section {
160
+
33
-
161
+ case .familySection:
162
+
34
-
163
+ label.text = model.FamilyName[indexPath.row]
164
+
165
+ label2.text = model.FamilyPhone[indexPath.row]
166
+
35
- image3.image = UIImage(data: model.FamilyPhotos[indexPath.row])
167
+ image3.image = UIImage(data: model.FamilyPhotos[indexPath.row])
168
+
169
+ // guard let uiImage = UIImage(data: model.FamilyPhotos[indexPath.row]), let cgImage = uiImage.cgImage else { return UICollectionViewCell() }
170
+
171
+ // image3.image = UIImage(cgImage: cgImage, scale: 0, orientation: uiImage.imageOrientation)
172
+
173
+ }
174
+
175
+
176
+
177
+ nameCell.delegate = self
178
+
179
+
180
+
181
+ return nameCell
182
+
183
+ }
184
+
185
+ func numberOfSections(in collectionView: UICollectionView) -> Int {
186
+
187
+ return 1
188
+
189
+ }
190
+
191
+ func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
192
+
193
+ switch model.section {
194
+
195
+ case .familySection:
196
+
197
+ return model.FamilyName.count
198
+
199
+ }
200
+
201
+ }
202
+
203
+ }
204
+
205
+ extension CardListViewController: UIAdaptivePresentationControllerDelegate {
206
+
207
+ func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
208
+
209
+ collectionView.reloadData()
210
+
211
+ }
212
+
213
+ }
214
+
215
+ extension CardListViewController: CollectionViewCellDelegate {
216
+
217
+ func delete(cell: CollectionViewCell) {
218
+
219
+ if let indexPath = collectionView?.indexPath(for: cell) {
220
+
221
+ switch model.section {
222
+
223
+ case .familySection:
224
+
225
+ model.FamilyPhotos.remove(at: indexPath.item)
226
+
227
+ model.FamilyName.remove(at: indexPath.item)
228
+
229
+ model.FamilyPhone.remove(at: indexPath.item)
230
+
231
+ }
232
+
233
+ collectionView?.deleteItems(at: [indexPath])
234
+
235
+ }
236
+
237
+ switch model.section {
238
+
239
+ case .familySection:
240
+
241
+ UserDefaults.standard.set(model.FamilyName, forKey: "Family1")
242
+
243
+ UserDefaults.standard.set(model.FamilyPhone, forKey: "Family2")
244
+
245
+ UserDefaults.standard.set(model.FamilyPhotos, forKey: "Family3")
246
+
247
+ }
248
+
249
+ collectionView.reloadData()
250
+
251
+ }
252
+
253
+ }
36
254
 
37
255
  ```
38
256
 
39
257
  ```ChildSettingViewController
40
258
 
259
+ import UIKit
260
+
261
+ import CropViewController
262
+
263
+
264
+
265
+ class ChildSettingViewController: UIViewController, UINavigationControllerDelegate {
266
+
267
+
268
+
269
+ @IBOutlet weak var NewBfName: UITextField!
270
+
271
+ @IBOutlet weak var NewBfImage: UIImageView!
272
+
273
+ @IBOutlet weak var NewBfTel: UITextField!
274
+
275
+
276
+
41
- var selectedImageData: Data?
277
+ var selectedImageData: Data?
278
+
42
-
279
+ var model: TitleListModel!
280
+
43
-
281
+ var image:UIImage?
282
+
283
+
284
+
44
-
285
+ override func viewDidLoad() {
286
+
287
+ super.viewDidLoad()
288
+
289
+ self.NewBfName.delegate = self
290
+
291
+ self.NewBfTel.delegate = self
292
+
293
+ }
294
+
295
+ @IBAction func NewBfButton(_ sender: Any) {
296
+
297
+ guard let text = NewBfName.text, !text.isEmpty,
298
+
299
+ let text1 = NewBfTel.text, !text1.isEmpty,
300
+
45
- selectedImageData = selectedImage.pngData()
301
+ let text3 = selectedImageData, !text3.isEmpty else { return }
302
+
46
-
303
+ switch self.model.section {
47
-
48
-
304
+
49
- //配列に入れる
305
+ case .familySection:
306
+
50
-
307
+ model.FamilyName.append(NewBfName.text!)
308
+
309
+ model.FamilyPhone.append(NewBfTel.text!)
310
+
51
- model.FamilyPhotos.append(selectedImageData!)
311
+ model.FamilyPhotos.append(selectedImageData!)
312
+
313
+ UserDefaults.standard.set(model.FamilyName, forKey: "Family1")
314
+
315
+ UserDefaults.standard.set(model.FamilyPhone, forKey: "Family2")
316
+
317
+ UserDefaults.standard.set(model.FamilyPhotos, forKey: "Family3")
318
+
319
+ }
320
+
321
+ }
322
+
323
+ }
324
+
325
+
326
+
327
+ extension ChildSettingViewController: UIImagePickerControllerDelegate {
328
+
329
+ func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
330
+
331
+
332
+
333
+ guard let pickerImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else { return }
334
+
335
+ selectedImageData = pickerImage.pngData()
336
+
337
+ let cropController = CropViewController(croppingStyle: .default, image: pickerImage)
338
+
339
+ cropController.delegate = self
340
+
341
+ cropController.customAspectRatio = NewBfImage.frame.size
342
+
343
+ cropController.cropView.cropBoxResizeEnabled = false
344
+
345
+ dismiss(animated: true, completion: nil)
346
+
347
+ self.present(cropController, animated: true, completion: nil)
348
+
349
+ }
350
+
351
+
352
+
353
+ @IBAction func BfImageButton(_ sender: Any) {
354
+
355
+ let alertController = UIAlertController(title: "確認", message: "選択してください", preferredStyle: .actionSheet)
356
+
357
+ if UIImagePickerController.isSourceTypeAvailable(.camera) {
358
+
359
+ let cameraAction = UIAlertAction(title: "カメラ", style: .default, handler: { (action) in
360
+
361
+ let imagePickerController = UIImagePickerController()
362
+
363
+ imagePickerController.sourceType = .camera
364
+
365
+ imagePickerController.delegate = self
366
+
367
+ self.present(imagePickerController, animated: true, completion: nil)
368
+
369
+ })
370
+
371
+ alertController.addAction(cameraAction)
372
+
373
+ }
374
+
375
+ if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
376
+
377
+ let photoLibraryAction = UIAlertAction(title: "フォトライブラリー", style: .default, handler: { (action) in
378
+
379
+ let imagePickerController = UIImagePickerController()
380
+
381
+ imagePickerController.sourceType = .photoLibrary
382
+
383
+ imagePickerController.delegate = self
384
+
385
+ self.present(imagePickerController, animated: true, completion: nil)
386
+
387
+ })
388
+
389
+ alertController.addAction(photoLibraryAction)
390
+
391
+ }
392
+
393
+ let cancelAction = UIAlertAction(title: "キャンセル", style: .cancel, handler: nil)
394
+
395
+ alertController.addAction(cancelAction)
396
+
397
+
398
+
399
+ present(alertController, animated: true, completion: nil)
400
+
401
+ }
402
+
403
+ }
404
+
405
+ extension ChildSettingViewController: CropViewControllerDelegate {
406
+
407
+ func cropViewController(_ cropViewController: CropViewController, didCropToImage image: UIImage, withRect cropRect: CGRect, angle: Int) {
408
+
409
+ updateImageViewWithImage(image, fromCropViewController: cropViewController)
410
+
411
+ }
412
+
413
+
414
+
415
+ func updateImageViewWithImage(_ image: UIImage, fromCropViewController cropViewController: CropViewController) {
416
+
417
+ self.NewBfImage.image = image
418
+
419
+ cropViewController.dismiss(animated: true, completion: nil)
420
+
421
+ }
422
+
423
+ }
424
+
425
+ extension ChildSettingViewController {
426
+
427
+ override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
428
+
429
+ super.dismiss(animated: flag, completion: completion)
430
+
431
+ guard let presentationController = presentationController else { return }
432
+
433
+ presentationController.delegate?.presentationControllerDidDismiss?(presentationController)
434
+
435
+ }
436
+
437
+ }
52
438
 
53
439
 
54
440