#CollectionViewにアルバムから選んだImageViewを表示したいのですが表示されません。
#####原因として考えられることは「配列の中身が空であること」「画像表示のコードが正しく記入されてないこと」だと思いますが、書き換え方がわかりません。
お手数おかけしますが、どなたかわかる方いらっしゃいましたら教えていただけると幸いです。
swift
1import UIKit 2import Photos 3import DKImagePickerController 4 5class MakeViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { 6 @IBOutlet var collectionView : UICollectionView! 7 private let reuseIdentifier = "Cell" 8 9 var imageArray : [UIImage] = [UIImage]() 10 let saveData = UserDefaults.standard 11 12 //画像加工するための元となる画像 13 var originalImage: UIImage! 14 //画像加工するフィルターの宣言 15 var filter: CIFilter! 16 17 override func viewDidLoad() { 18 super.viewDidLoad() 19 20 //imageArrayに写真を入れていく 21 imageArray = [UIImage]() 22 23 //UICollectionViewFlowLayoutをインスタンス化 24 let layout = UICollectionViewFlowLayout() 25 layout.sectionInset = UIEdgeInsets(top: 15, left: 5, bottom: 15, right: 5)//レイアウトを調整 26 layout.minimumInteritemSpacing = 0 27 collectionView.collectionViewLayout = layout 28 self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell") 29 30 collectionView.delegate = self 31 collectionView.dataSource = self 32 } 33 34 override func didReceiveMemoryWarning() { 35 super.didReceiveMemoryWarning() 36 } 37 38 override func viewWillAppear(_ animated: Bool){ 39 super.viewWillAppear(true) 40 if saveData.array(forKey: "Image") != nil{ 41 imageArray = saveData.array(forKey: "Image")as![UIImage] 42 } 43 collectionView.reloadData() 44 } 45 //セクション数を指定します 46 func numberOfSections(in collectionView: UICollectionView) -> Int { 47 return 1 48 } 49 50 //セルの個数を指定します 51 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 52 return imageArray.count 53 } 54 //セルの中身の表示の仕方を設定します 55 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 56 //表示するCellの登録 57 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MyCustomCell 58 59 cell.cameraImageView.image = imageArray[indexPath.row] 60 61 return cell 62 } 63 64 //セルの配置について決める 65 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 66 let horizontalSpace : CGFloat = 10 67 let cellSize : CGFloat = self.view.bounds.width / 3 - horizontalSpace 68 return CGSize(width: cellSize, height: cellSize) 69 } 70 71 72 //カメラロールにある画像を読み込むメソッド 73 @IBAction func openAlbum(){ 74 75 let MyCustomCell = DKImagePickerController() 76 // 選択可能な枚数を9にする 77 MyCustomCell.maxSelectableCount = 9 78 MyCustomCell.didSelectAssets = { [unowned self] (assets: [DKAsset]) in 79 80 // 選択された画像はassetsに入れて返却されるのでfetchして取り出す 81 for asset in assets { 82 asset.fetchFullScreenImage(completeBlock: { (image, info) in 83 // ここで取り出せる 84 self.cameraImageView.image = image ←←←ここでエラーが発生します 85 86 }) 87 } 88 } 89 self.present(MyCustomCell, animated: true) {} 90 } 91 92 @IBAction func back(){ 93 self.dismiss(animated: true, completion: nil) 94 } 95} 96
回答1件
あなたの回答
tips
プレビュー