CollectionViewのセルにPhotosで取得したカメラロールの写真を表示しようとしています。
その際に以下のようなコードを書いているのですが、CollectionViewに写真が表示されません。
import UIKit import Photos class UserPhotosCollectionView: UICollectionView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { var photoAssets = [PHAsset]() override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) { super.init(frame: frame, collectionViewLayout: layout) //デリゲート self.delegate = self self.dataSource = self //セルを登録 self.registerNib(UINib(nibName: "UserPhotosCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "UserPhotosCollectionViewCell") self.backgroundColor = UIColor.whiteColor() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func numberOfSections() -> Int { return 1 } //呼ばれてない func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = dequeueReusableCellWithReuseIdentifier("UserPhotosCollectionViewCell", forIndexPath: indexPath) as! UserPhotosCollectionViewCell let asset = photoAssets[indexPath.row] let manager = PHImageManager() manager.requestImageForAsset(asset, targetSize: CGSize(width: 70, height: 70), contentMode: .AspectFill, options: nil) { (image, info) -> Void in cell.photoImage.image = image } return cell } //呼ばれている func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return photoAssets.count }
import UIKit import Photos class ChoosePhotoViewController: UIViewController { var photoAssets = [PHAsset]() override func viewDidLoad() { super.viewDidLoad() self.checkAuthorizationStatus() self.view.backgroundColor = UIColor.lightGrayColor() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //フォトライブラリへのアクセス許可を求める private func checkAuthorizationStatus() { let status = PHPhotoLibrary.authorizationStatus() switch status { case .Authorized: self.getAllPhotosInfo() default: PHPhotoLibrary.requestAuthorization({ (status) -> Void in if status == .Authorized { self.getAllPhotosInfo() } }) } } //フォトライブラリから全ての写真のPHAssetオブジェクトを取得する private func getAllPhotosInfo() { photoAssets = [] //ソート条件を指定 let options = PHFetchOptions() options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)] let assets: PHFetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: options) assets.enumerateObjectsUsingBlock { (asset, index, stop) -> Void in self.photoAssets.append(asset as! PHAsset) let frame = CGRect(x: 0, y: self.view.frame.height - self.view.frame.height / 2, width: self.view.frame.width, height: self.view.frame.height / 2) let layout = UICollectionViewLayout() let userPhotosCollectionView = UserPhotosCollectionView(frame: frame, collectionViewLayout: layout) print(userPhotosCollectionView) userPhotosCollectionView.photoAssets = self.photoAssets self.view.addSubview(userPhotosCollectionView) } } }
デバッグエリアには以下のように表示されています。
<InstagramApp.UserPhotosCollectionView: 0x7f9e6408be00; baseClass = UICollectionView; frame = (0 333.5; 375 333.5); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7f9e6269ce80>; layer = <CALayer: 0x7f9e6267add0>; contentOffset: {0, 0}; contentSize: {0, 0}> collection view layout: <UICollectionViewLayout: 0x7f9e626777a0> <InstagramApp.UserPhotosCollectionView: 0x7f9e6405a600; baseClass = UICollectionView; frame = (0 333.5; 375 333.5); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7f9e626b2b40>; layer = <CALayer: 0x7f9e626b2670>; contentOffset: {0, 0}; contentSize: {0, 0}> collection view layout: <UICollectionViewLayout: 0x7f9e626b1eb0> <InstagramApp.UserPhotosCollectionView: 0x7f9e640a8000; baseClass = UICollectionView; frame = (0 333.5; 375 333.5); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7f9e626b4b40>; layer = <CALayer: 0x7f9e626b4290>; contentOffset: {0, 0}; contentSize: {0, 0}> collection view layout: <UICollectionViewLayout: 0x7f9e626b4540> <InstagramApp.UserPhotosCollectionView: 0x7f9e640a8800; baseClass = UICollectionView; frame = (0 333.5; 375 333.5); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7f9e626b6840>; layer = <CALayer: 0x7f9e626b6090>; contentOffset: {0, 0}; contentSize: {0, 0}> collection view layout: <UICollectionViewLayout: 0x7f9e626b6340> <InstagramApp.UserPhotosCollectionView: 0x7f9e62884c00; baseClass = UICollectionView; frame = (0 333.5; 375 333.5); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7f9e624d4f70>; layer = <CALayer: 0x7f9e624d52f0>; contentOffset: {0, 0}; contentSize: {0, 0}> collection view layout: <UICollectionViewLayout: 0x7f9e62487ec0>
あなたの回答
tips
プレビュー