###前提・実現したいこと
[iOS swift]UICollectionView、PHAssetを使ったカメラロールの表示
セルにカメラロールの画像を入れこむことは下記のコードで実現できたのですが、メモリの解放がうまくできません。
###発生している問題・エラーメッセージ
画像が1000枚程度カメラロールにあり、初期表示は問題ないが素早くスクロールをするとアプリケーションが落ちてしまします。
正しい表示の方法、メモリの解放等ご教授いただけたらと思います。
よろしくお願いします。
Received memory warning.
###該当のソースコード
swift
1import UIKit 2import Photos 3 4class albumViewController2: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate { 5 6 var photoAssets = [PHAsset]() 7 var photoImages = [UIImage?]() 8 var photoCounts:Int = 0 9 var collectionView:UICollectionView! 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 let screenWidth = CGFloat( UIScreen.mainScreen().bounds.size.width) 14 15 self.view.backgroundColor = UIColor.grayColor() 16 17 //カメラロールから画像取得 18 getAllPhotosInfo() 19 20 //レイアウト部 21 let flowLayout = UICollectionViewFlowLayout() 22 flowLayout.scrollDirection = .Vertical 23 flowLayout.minimumInteritemSpacing = 0.0 24 flowLayout.minimumLineSpacing = 0.0 25 flowLayout.itemSize = CGSizeMake(screenWidth / 4, screenWidth / 4) 26 27 // コレクションビュー作成 28 collectionView = UICollectionView(frame: view.frame, collectionViewLayout: flowLayout) 29 collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell") 30 collectionView.dataSource = self 31 collectionView!.delegate = self 32 collectionView.backgroundColor = UIColor.clearColor() 33 view.addSubview(collectionView) 34 } 35 36 override func didReceiveMemoryWarning() { 37 super.didReceiveMemoryWarning() 38 } 39 40 41 func getAllPhotosInfo() { 42 photoAssets = [] 43 44 // ソート条件を指定 45 let options = PHFetchOptions() 46 options.sortDescriptors = [ 47 NSSortDescriptor(key: "creationDate", ascending: false) 48 ] 49 50 // 画像すべてのAssetを取得 51 let assets: PHFetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: options) 52 assets.enumerateObjectsUsingBlock { (asset, index, stop) -> Void in 53 self.photoAssets.append(asset as! PHAsset) 54 } 55 photoCounts = photoAssets.count 56 } 57 58 59 //コレクションビュー 60 func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 61 return 1 62 } 63 64 func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 65 return photoCounts 66 } 67 68 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 69 let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as UICollectionViewCell 70 71 cell.backgroundColor = UIColor.brownColor() 72 //PHAseetから画像取得のリクエストを投げてcellにセット 73 let asset = photoAssets[indexPath.row] 74 var photo: UIImage? 75 let manager: PHImageManager = PHImageManager() 76 77 78 for subview in cell.contentView.subviews{ 79 subview.removeFromSuperview() 80 } 81 manager.requestImageForAsset(asset, 82 targetSize: CGSizeMake(CGFloat(asset.pixelWidth) , CGFloat(asset.pixelHeight) ), 83 contentMode: .AspectFill, 84 options: nil) { (image, info) -> Void in 85 photo = image! 86 } 87 88 let imageview:UIImageView=UIImageView(frame: CGRectMake(1, 1, (self.view.bounds.width / 4) - 2, (self.view.bounds.width / 4) - 2 )) 89 imageview.image = photo 90 imageview.contentMode = .Center 91 imageview.clipsToBounds = true 92 cell.contentView.addSubview(imageview) 93 94 return cell 95 96 } 97}
###補足情報
Xcode 7.2.1 swift
回答1件
あなたの回答
tips
プレビュー