###前提・実現したいこと
カメラロールに保存されてる写真をCollectionViewを使い、実行したいと思ってます
###現在起きている問題
現在、runしても画面が真っ白なままで、変化がありません
また、コンソールには何も書かれていません
###該当のソースコード
//![![ViewCo](ddb8b29e85a7f097dd09cbeb29089f02.png)](6ac86430604142fa0dbe9adabd689307.png)ntroller import UIKit import Photos class ViewController: UIViewController { @IBOutlet weak var collectionView: UICollectionView! var photoAssets: Array! = [PHAsset]() override func viewDidLoad() { super.viewDidLoad() setup() libraryRequestAuthorization() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } fileprivate func setup() { collectionView.dataSource = self // UICollectionViewCellのマージン等の設定 let flowLayout: UICollectionViewFlowLayout! = UICollectionViewFlowLayout() flowLayout.itemSize = CGSize(width: UIScreen.main.bounds.width / 3 - 4, height: UIScreen.main.bounds.width / 3 - 4) flowLayout.sectionInset = UIEdgeInsets(top: 8, left: 0, bottom: 0, right: 0) flowLayout.minimumInteritemSpacing = 0 flowLayout.minimumLineSpacing = 6 collectionView.collectionViewLayout = flowLayout } // カメラロールへのアクセス許可 fileprivate func libraryRequestAuthorization() { PHPhotoLibrary.requestAuthorization({ [weak self] status in guard let wself = self else { return } switch status { case .authorized: wself.getAllPhotosInfo() case .denied: wself.showDeniedAlert() case .notDetermined: print("NotDetermined") case .restricted: print("Restricted") } }) } // カメラロールから全て取得する fileprivate func getAllPhotosInfo() { let assets: PHFetchResult = PHAsset.fetchAssets(with: .image, options: nil) assets.enumerateObjects({ [weak self] (asset, index, stop) -> Void in guard let wself = self else { return } wself.photoAssets.append(asset as PHAsset) }) collectionView.reloadData() } // カメラロールへのアクセスが拒否されている場合のアラート fileprivate func showDeniedAlert() { let alert: UIAlertController = UIAlertController(title: "エラー", message: "「写真」へのアクセスが拒否されています。設定より変更してください。", preferredStyle: .alert) let cancel: UIAlertAction = UIAlertAction(title: "キャンセル", style: .cancel, handler: nil) let ok: UIAlertAction = UIAlertAction(title: "設定画面へ", style: .default, handler: { [weak self] (action) -> Void in guard let wself = self else { return } wself.transitionToSettingsApplition() }) alert.addAction(cancel) alert.addAction(ok) present(alert, animated: true, completion: nil) } fileprivate func transitionToSettingsApplition() { let url = URL(string: UIApplicationOpenSettingsURLString) if let url = url { UIApplication.shared.open(url, options: [:], completionHandler: nil) } } } extension ViewController : UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return photoAssets.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CameraRollCollectionViewCell cell.setConfigure(assets: photoAssets[indexPath.row]) return cell } }
//CameraRollCollectionViewCell import UIKit import Photos class CameraRollCollectionViewCell: UICollectionViewCell { @IBOutlet weak var photoImageView: UIImageView! override func awakeFromNib() { super.awakeFromNib() } // 画像を表示する func setConfigure(assets: PHAsset) { let manager = PHImageManager() manager.requestImage(for: assets, targetSize: frame.size, contentMode: .aspectFill, options: nil, resultHandler: { [weak self] (image, info) in guard let wself = self, let outImage = image else { return } self?.photoImageView.image = image }) } }
###確認したこと
関連付け、 UICollectionViewCell のIdentifierはCellになっており。
また、セルのクラスも CameraRollCollectionViewCell と指定しています。また、info.plistにも書いていいます。
違っているところがあれば教えてくださると幸いです
###開発環境など
xcode8
swift3
iPhoneアプリ