Realmで保存したString型の配列にある写真の値をloop文で回しながらUIImage型に変換して、UICollectionViewCellに値を格納したい。
こんにちは、質問失礼致します。
現在、Realmのファイルに保存されているデータを取得し、そのデータの中に保存されているimageString
という変数に写真のデータがString型の配列で保存されているので、String型の配列にあるデータをloop文で回し、 base64
を使用してUIImage型の配列に変換し、UICollectionViewのcellにUIImageViewがあるので、そこのimageViewに値を入れたいと考えています。
該当のソースコード
swift
1import UIKit 2import RealmSwift 3import SVProgressHUD 4 5class CategoryPhotoUpViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { 6 @IBOutlet weak var categoryCollectionView: UICollectionView! 7 8 let realm = try! Realm() 9 10 var morningArray = try! Realm().objects(Morning.self).sorted(byKeyPath: "id", ascending: false) 11 12 var categoryXib: XibCategoryPhotoView! 13 14 override func viewDidLoad() { 15 16 // もしmorningArrayの数が0だったら遷移元の画面に戻る 17 if morningArray.count == 0 { 18 self.navigationController?.popViewController(animated: true) 19 } 20 print("DEBUG_PRINT: morningControllerが表示されました") 21 print("DEBUG_PRINT: (morningArray.count)") 22 categoryCollectionView.delegate = self 23 categoryCollectionView.dataSource = self 24 25 categoryXib = XibCategoryPhotoView() 26 categoryCollectionView.reloadData() 27 28 } 29 30 override func didReceiveMemoryWarning() { 31 } 32 33 override func viewWillAppear(_ animated: Bool) { 34 categoryCollectionView.reloadData() 35 print("DEBUG_PRINT: 画面を更新します") 36 } 37 38 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 39 print("DEBUG_PRINT: 数を返します") 40 return morningArray.count 41 } 42 43 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 44 let cell = categoryCollectionView.dequeueReusableCell(withReuseIdentifier: "Cell2", for: indexPath) as! CategoryCollectionViewCell 45 if morningArray.count == 0 { 46 print("DEBUG_PRINT: morningArrayのデータが0です") 47 } else if morningArray.count > 0 { 48 print("DEBUG_PRINT: morningArrayのデータが0以上です") 49 50 for morningArrayValue in morningArray { 51 for imageStringer in morningArrayValue.imageString { 52 var image: UIImage 53 image = UIImage(data: Data(base64Encoded: String(imageStringer), options: .ignoreUnknownCharacters)!)! 54 55 cell.imageView.image = image 56 print("DEBUG_PRINT: cellのイメージに値をセットしました。") 57 print("DEBUG_PRINT: (String(describing: cell.imageView.image))") 58 } 59 } 60 } 61 return cell 62 } 63 64 // セルサイズの自動変更を設定する 65 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 66 let width: CGFloat = view.frame.width / 3 - 1 // self.viewを/3し、-1は隙間の部分 67 let height: CGFloat = width 68 69 return CGSize(width: width, height: height) 70 } 71}
試したこと
for loop文でRealmからデータを取得している変数 morningArray
を回しており、次のfor文で morningArray
から取得したデータ morningArrayValue
に imageString
のデータを与え回しています。変数 imageStringer
に base64
を使用しUIImage型に変換しようと考えています.
シグバートエラーが image = UIImage(data: Data(base64Encoded: String(imageStringer), options: .ignoreUnknownCharacters)!)!
の部分で出ています。
デバックエリアはこのようになっていて、imageに何かしらの値は入ってるように感じるのですが、imageにString型のデータをUIImage型に変換して入れようとするところで止まってしまいます。これはなぜ変数imageにUIImage型で値が入らないのでしょうか?基本的には1つ上の部分のif文で morningArray
の数が0以上であることは確認してあるのでnilが入ることはないと思ってるのですが、何かコードに不備があるのでしょうか?
ご存知の方がいらっしゃったらご教授いただけるとありがたいです。
初心者なので至らない点もあると思いますがよろしくお願い致します。
補足情報(FW/ツールのバージョンなど)
Xcode Version 9.3.1
よろしくお願い致します。
追記
import RealmSwift class Morning: Object { // 管理用 ID。プライマリーキー @objc dynamic var id = 0 // 投稿ID @objc dynamic var postID = "" // UserName @objc dynamic var userName = "" // ImageString @objc dynamic var imageString = "" // caption @objc dynamic var caption = "" // date @objc dynamic var time = "" override static func primaryKey() -> String? { return "id" } }
// Date.timeIntervalSinceReferenceDateメソッドだけを取り出し、コードの量を減らす let timer = Date.timeIntervalSinceReferenceDate // もしもMorningSwitchがtrueだったら if morningSwitch.isOn == true { let morning = Morning() morning.time = String(timer) let morningArray = realm.objects(Morning.self) // もしもmorningArrayのcountプロパティが0じゃなかったら if morningArray.count != 0 { morning.id = morningArray.max(ofProperty: "id")! + 1 } try! realm.write { // タグmorningに投稿情報が保存される時 morning.caption = self.textField.text! morning.userName = (Auth.auth().currentUser?.displayName!)! morning.imageString = imageData!.base64EncodedString(options: .lineLength64Characters) // 日付の値を取得する morning.time = String(timer) realm.add(morning, update: true) } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/05/14 02:59 編集
2018/05/14 04:01
退会済みユーザー
2018/05/14 08:23
2018/05/14 08:42 編集
退会済みユーザー
2018/05/14 11:09
2018/05/14 12:05
退会済みユーザー
2018/05/14 13:06 編集
2018/05/14 12:40
退会済みユーザー
2018/05/14 12:57
2018/05/14 12:59
退会済みユーザー
2018/05/14 13:16
2018/05/14 13:33
退会済みユーザー
2018/05/14 13:58
2018/05/15 02:46
退会済みユーザー
2018/05/15 03:07