Realmで保存したString型の配列にある写真の値をUIImage型に変換し、loop文で回し、UICollectionViewのセルに格納したい
こんにちは、初めて。terataildで質問させていただくプログラミング歴3ヶ月の初心者です。勉強不足で至らない点があると思いますが、よろしくお願い致します。
現在、Realmのファイルに保存されているデータを取得し、そのデータの中に保存されているimageString
という変数に写真のデータがString型の配列で保存されているので、String型のデータ配列にあるデータをloop文で回し、 base64
を使用してUIImage型の配列に変換し、UICollectionViewのcellにUIImageViewがあるので、そこのimageViewに値を入れたいと考えています。
Type of expression is ambiguous without more context
該当のソースコード
swift
1import UIKit 2import RealmSwift 3 4class CategoryPhotoUpViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { 5 @IBOutlet weak var categoryCollectionView: UICollectionView! 6 7 let realm = try! Realm() 8 9 var morningArray = try! Realm().objects(Morning.self).sorted(byKeyPath: "id", ascending: false) 10 11 override func viewDidLoad() { 12 categoryCollectionView.delegate = self 13 categoryCollectionView.dataSource = self 14 } 15 16 override func didReceiveMemoryWarning() { 17 } 18 19 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 20 return morningArray.count 21 } 22 23 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 24 let cell = categoryCollectionView.dequeueReusableCell(withReuseIdentifier: "Cell2", for: indexPath) as! CategoryCollectionViewCell 25 26 for morningArrayValue in morningArray { 27 for imageStringer in morningArrayValue.imageString { 28 var image: UIImage? 29 image = UIImage(data: Data(base64Encoded: imageStringer, options: .ignoreUnknownCharacters)!) 30 cell.imageView.image = image! 31 } 32 } 33 return cell 34 } 35 36}
試したこと
for loop文でRealmからデータを取得している変数 morningArray
を回しており、次のfor文で morningArray
から取得したデータ morningArrayValue
に imageString
のデータを与え回しています。変数 imageStringer
に base64
を使用しUIImage型に変換しようと考えています。エラーメッセージである Type of expression is ambiguous without more context
はDataの部分で出ている状況でQiitaやGoogleを使用し、調べていますが型が不明瞭ということで少し理解できずにいます。
どなたかご存知あればご教授いただけたら幸いです。
補足情報(FW/ツールのバージョンなど)
Xcode Version 9.2
よろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。