質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

0回答

773閲覧

【swift】画像の配列への入れ方がわからない

yoppen9

総合スコア0

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2021/09/17 05:59

編集2021/09/28 01:00

加工した画像を配列に入れてcollectionViewに並べたい

トリミング完了画像を配列に入れてcollectionViewにて配列内の画像を並べたいのですが、現在UIImageをpngDataにして配列に入れてます。ですがcollectionViewに表示すると、トリミング前の画像が並んでしまいます。
トリミング完了後の画像を上手くcollectionViewに表示する方法はありますか?

説明が上手く出来ず申し訳ありません。理解できないところは質問してくれると幸いです。

宜しくお願い致します。

該当のソースコード

TitleListModel

1import Foundation 2 3enum Section { 4 case familySection 5} 6 7class TitleListModel { 8 var sectionTitle: String = "家族" 9 var section = Section.familySection 10 let cardsList = ["家族"] 11 12 var FamilyName: [String] = [] 13 var FamilyPhone: [String] = [] 14 var FamilyPhotos: [Data] = [] 15}

CardListViewController

1import UIKit 2 3class CardListViewController: UIViewController, UIGestureRecognizerDelegate { 4 5 @IBOutlet weak var collectionView: UICollectionView! 6 @IBOutlet weak var collectionViewFlowLayout: UICollectionViewFlowLayout! 7 @IBOutlet weak var addBarButtonItem: UIBarButtonItem! 8 9 var model: TitleListModel! 10 11 override func loadView() { 12 super.loadView() 13 14 switch model.section { 15 16 case .familySection: 17 if(UserDefaults.standard.array(forKey: "Family1") != nil) { 18 model.FamilyName = UserDefaults.standard.array(forKey: "Family1") as! [String] 19 } 20 if(UserDefaults.standard.array(forKey: "Family2") != nil) { 21 model.FamilyPhone = UserDefaults.standard.array(forKey: "Family2") as! [String] 22 } 23 if(UserDefaults.standard.array(forKey: "Family3") != nil) { 24 model.FamilyPhotos = UserDefaults.standard.array(forKey: "Family3") as! [Data] 25 } 26 } 27 } 28 override func viewDidLoad() { 29 super.viewDidLoad() 30 31 collectionView.delegate = self 32 collectionView.dataSource = self 33 34 let nib = UINib(nibName: "CollectionViewCell", bundle: .main) 35 collectionView.register(nib, forCellWithReuseIdentifier: "Cell1") 36 } 37 @IBAction func settingButton(_ sender: Any) { 38 let nextView2 = storyboard?.instantiateViewController(identifier: "next2") as! ChildSettingViewController 39 nextView2.modalPresentationStyle = .fullScreen 40 nextView2.presentationController?.delegate = self 41 nextView2.model = self.model 42 present(nextView2, animated: true, completion: nil) 43 } 44} 45 46extension CardListViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { 47 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 48 let nameCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell1", for: indexPath) as! CollectionViewCell 49 let label = nameCell.contentView.viewWithTag(1) as! UILabel 50 let label2 = nameCell.contentView.viewWithTag(2) as! UILabel 51 let image3 = nameCell.contentView.viewWithTag(3) as! UIImageView 52 53 switch model.section { 54 case .familySection: 55 label.text = model.FamilyName[indexPath.row] 56 label2.text = model.FamilyPhone[indexPath.row] 57 image3.image = UIImage(data: model.FamilyPhotos[indexPath.row]) 58 } 59 nameCell.delegate = self 60 61 return nameCell 62 } 63 func numberOfSections(in collectionView: UICollectionView) -> Int { 64 return 1 65 } 66 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 67 switch model.section { 68 case .familySection: 69 return model.FamilyName.count 70 } 71 } 72} 73extension CardListViewController: UIAdaptivePresentationControllerDelegate { 74 func presentationControllerDidDismiss(_ presentationController: UIPresentationController) { 75 collectionView.reloadData() 76 } 77} 78extension CardListViewController: CollectionViewCellDelegate { 79 func delete(cell: CollectionViewCell) { 80 if let indexPath = collectionView?.indexPath(for: cell) { 81 switch model.section { 82 case .familySection: 83 model.FamilyPhotos.remove(at: indexPath.item) 84 model.FamilyName.remove(at: indexPath.item) 85 model.FamilyPhone.remove(at: indexPath.item) 86 } 87 collectionView?.deleteItems(at: [indexPath]) 88 } 89 switch model.section { 90 case .familySection: 91 UserDefaults.standard.set(model.FamilyName, forKey: "Family1") 92 UserDefaults.standard.set(model.FamilyPhone, forKey: "Family2") 93 UserDefaults.standard.set(model.FamilyPhotos, forKey: "Family3") 94 } 95 collectionView.reloadData() 96 } 97}

ChildSettingViewController

1import UIKit 2import CropViewController 3 4class ChildSettingViewController: UIViewController, UINavigationControllerDelegate { 5 6 @IBOutlet weak var NewBfName: UITextField! 7 @IBOutlet weak var NewBfImage: UIImageView! 8 @IBOutlet weak var NewBfTel: UITextField! 9 10 var selectedImageData: Data? 11 var model: TitleListModel! 12 var image:UIImage? 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 self.NewBfName.delegate = self 17 self.NewBfTel.delegate = self 18 } 19 @IBAction func NewBfButton(_ sender: Any) { 20 guard let text = NewBfName.text, !text.isEmpty, 21 let text1 = NewBfTel.text, !text1.isEmpty, 22 let text3 = selectedImageData, !text3.isEmpty else { return } 23 switch self.model.section { 24 case .familySection: 25 model.FamilyName.append(NewBfName.text!) 26 model.FamilyPhone.append(NewBfTel.text!) 27 model.FamilyPhotos.append(selectedImageData!) 28 UserDefaults.standard.set(model.FamilyName, forKey: "Family1") 29 UserDefaults.standard.set(model.FamilyPhone, forKey: "Family2") 30 UserDefaults.standard.set(model.FamilyPhotos, forKey: "Family3") 31 } 32 } 33} 34 35extension ChildSettingViewController: UIImagePickerControllerDelegate { 36 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { 37 38 guard let pickerImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else { return } 39 selectedImageData = pickerImage.pngData() 40 let cropController = CropViewController(croppingStyle: .default, image: pickerImage) 41 cropController.delegate = self 42 cropController.customAspectRatio = NewBfImage.frame.size 43 cropController.cropView.cropBoxResizeEnabled = false 44 dismiss(animated: true, completion: nil) 45 self.present(cropController, animated: true, completion: nil) 46 } 47 48 @IBAction func BfImageButton(_ sender: Any) { 49 let alertController = UIAlertController(title: "確認", message: "選択してください", preferredStyle: .actionSheet) 50 if UIImagePickerController.isSourceTypeAvailable(.camera) { 51 let cameraAction = UIAlertAction(title: "カメラ", style: .default, handler: { (action) in 52 let imagePickerController = UIImagePickerController() 53 imagePickerController.sourceType = .camera 54 imagePickerController.delegate = self 55 self.present(imagePickerController, animated: true, completion: nil) 56 }) 57 alertController.addAction(cameraAction) 58 } 59 if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) { 60 let photoLibraryAction = UIAlertAction(title: "フォトライブラリー", style: .default, handler: { (action) in 61 let imagePickerController = UIImagePickerController() 62 imagePickerController.sourceType = .photoLibrary 63 imagePickerController.delegate = self 64 self.present(imagePickerController, animated: true, completion: nil) 65 }) 66 alertController.addAction(photoLibraryAction) 67 } 68 let cancelAction = UIAlertAction(title: "キャンセル", style: .cancel, handler: nil) 69 alertController.addAction(cancelAction) 70 71 present(alertController, animated: true, completion: nil) 72 } 73} 74extension ChildSettingViewController: CropViewControllerDelegate { 75 func cropViewController(_ cropViewController: CropViewController, didCropToImage image: UIImage, withRect cropRect: CGRect, angle: Int) { 76 updateImageViewWithImage(image, fromCropViewController: cropViewController) 77 } 78 79 func updateImageViewWithImage(_ image: UIImage, fromCropViewController cropViewController: CropViewController) { 80 self.NewBfImage.image = image 81 cropViewController.dismiss(animated: true, completion: nil) 82 } 83} 84extension ChildSettingViewController { 85 override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) { 86 super.dismiss(animated: flag, completion: completion) 87 guard let presentationController = presentationController else { return } 88 presentationController.delegate?.presentationControllerDidDismiss?(presentationController) 89 } 90} 91

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hoshi-takanori

2021/09/17 12:24

いまいち何がしたいのかよく分からないし、コードが断片的すぎてアドバイスのしようがありません。
yoppen9

2021/09/18 00:56

コード追加しました。 簡潔に言うと、フォトライブラリから画像を選びその画像を加工し配列に入れたいのです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問