前提・実現したいこと
Photos frameworkを使って端末内のアルバムの画像をUIImageの配列として取得したいです。
こちらを参考にして、実装を行ったのですが実行時にエラーで落ちてしまいます。
調べた限りメモリリークに関するエラーのようですが、解決方法がわからなかったので
ご教示頂ければ幸いです。
発生している問題・エラーメッセージ
2019-01-19 11:56:15.097534+0900 GemCamera[8198:2560115] [ImageManager] First stage of an opportunistic image request returned a non-table format image, this is not fatal, but it is unexpected Message from debugger: Terminated due to memory issue
該当のソースコード
Swift
1class AlbumViewController: UIViewController { 2 var assets:[PHAsset] = [] 3 var selectedAsset:PHAsset? = nil 4 5 @IBOutlet weak var albumCollectionView: GeminiCollectionView!{ 6 didSet{ 7 let cellIdentifier = "AlbumCollectionViewCell" 8 let nib = UINib(nibName: cellIdentifier, bundle: nil) 9 albumCollectionView.register(nib, forCellWithReuseIdentifier: cellIdentifier) 10 albumCollectionView.delegate = self 11 albumCollectionView.dataSource = self 12 13 albumCollectionView.gemini 14 .circleRotationAnimation() 15 .radius(450) 16 .rotateDirection(.clockwise) 17 .itemRotationEnabled(true) 18 } 19 } 20 21 override func viewDidLoad() { 22 super.viewDidLoad() 23 assets = getAssetsInAlbum() 24 25 if let layout = albumCollectionView.collectionViewLayout as? UICollectionViewFlowLayout { 26 layout.scrollDirection = .vertical 27 albumCollectionView.collectionViewLayout = layout 28 } 29 } 30 31 private func getAssetsInAlbum()->[PHAsset]{ 32 var assets:[PHAsset] = [] 33 34 let options = PHFetchOptions() 35 let fetchResult = PHAsset.fetchAssets(with: .image, options: options) 36 fetchResult.enumerateObjects { (asset, index, stop) -> Void in 37 assets.append(asset) 38 } 39 40 return assets 41 } 42 43 // MARK: - Navigation 44 45 // In a storyboard-based application, you will often want to do a little preparation before navigation 46 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 47 if segue.identifier == "toPhotoPreView"{ 48 let photoViewController = segue.destination as! PhotoPreViewController 49 photoViewController.selectedAsset = selectedAsset 50 } 51 } 52} 53 54// MARK: - UIScrollViewDelegate 55extension AlbumViewController { 56 func scrollViewDidScroll(_ scrollView: UIScrollView) { 57 albumCollectionView.animateVisibleCells() 58 } 59} 60 61// MARK: - UICollectionViewDelegate 62extension AlbumViewController: UICollectionViewDelegate { 63 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 64 selectedAsset = assets[indexPath.row] 65 performSegue(withIdentifier: "toPhotoPreView", sender: nil) 66 } 67 68 func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { 69 if let cell = cell as? GeminiCell { 70 self.albumCollectionView.animateCell(cell) 71 } 72 } 73} 74// MARK: - UICollectionViewDataSource 75extension AlbumViewController:UICollectionViewDataSource{ 76 func numberOfSections(in collectionView: UICollectionView) -> Int { 77 return 1 78 } 79 80 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 81 return assets.count 82 } 83 84 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 85 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AlbumCollectionViewCell", for: indexPath) as! AlbumCollectionViewCell 86 cell.configure(with: assets[indexPath.row]) 87 self.albumCollectionView.animateCell(cell) 88 return cell 89 } 90} 91 92// MARK: - UICollectionViewDelegateFlowLayout 93extension AlbumViewController: UICollectionViewDelegateFlowLayout { 94 private enum Const { 95 static let collcetionViewSize = CGSize(width: UIScreen.main.bounds.size.width * 0.7, height: UIScreen.main.bounds.size.height * 0.7) 96 } 97 98 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 99 return Const.collcetionViewSize 100 } 101 102 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { 103 guard let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else { 104 return UIEdgeInsets(top: 50, left: 50, bottom: 50, right: 50) 105 } 106 107 switch layout.scrollDirection { 108 case .horizontal: 109 110 let verticalMargin: CGFloat = (collectionView.bounds.height - Const.collcetionViewSize.height) / 2 111 return UIEdgeInsets(top: 50 + verticalMargin, 112 left: 50, 113 bottom: 50 + verticalMargin, 114 right: 50) 115 case .vertical: 116 117 let horizontalMargin: CGFloat = (collectionView.bounds.width - Const.collcetionViewSize.width) / 2 118 return UIEdgeInsets(top: 50, 119 left: 50 + horizontalMargin, 120 bottom: 50, 121 right: 50 + horizontalMargin) 122 } 123 } 124 125 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 126 return 10 127 } 128 129 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { 130 return 0 131 } 132}
class AlbumCollectionViewCell: GeminiCell { @IBOutlet weak var image: UIImageView! override func awakeFromNib() { super.awakeFromNib() // Initialization code } func configure(with asset:PHAsset ) { var photoImage:UIImage? = nil let targetSize = CGSize(width: self.bounds.width, height: self.bounds.height) let manager = PHImageManager() manager.requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFit,options: nil){ (image, info) in if image != nil{ photoImage = image } } self.image.image = photoImage } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/20 15:51
2019/01/20 16:14
2019/01/21 14:47
2019/01/22 15:04
2019/01/22 15:38 編集
2019/01/22 15:44
2019/01/23 13:32