###前提・実現したいこと
UICollectionViewでiPhone標準のフォトアプリを作ろうと思っています。
画像を複数選択し、任意のアルバムに追加するために選択した画像の情報を
確保したいのですが、選択されたセルの中に入ってある画像を随時確保する
方法が分からなくて困っています。
どなたか解決策をご教授頂けないでしょうか。
何卒よろしくお願いします。
###該当のソースコード
swift
1import UIKit 2import Photos 3 4class FirstViewController: UIViewController , UICollectionViewDelegate , UICollectionViewDataSource { 5 var checkArray:NSMutableArray = [] //選択されたセルの番号を記憶するための配列 6 var photoAssets = [PHAsset]() //画像を入れてる 7 var SelectphotoAsset = [PHAsset]() //選択されたセルに入っている画像のみをここに入れたい 8 9//iphone内の画像を取得するコード 10 private func getAllPhotosInfo() { 11 photoAssets = [] 12 13 var options = PHFetchOptions() 14 options.sortDescriptors = [ 15 NSSortDescriptor(key: "creationDate", ascending: false) 16 ] 17 18 var assets: PHFetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: options) 19 assets.enumerateObjectsUsingBlock { (asset, index, stop) -> Void in 20 self.photoAssets.append(asset as! PHAsset) 21 } 22 print(photoAssets) 23 } 24//セルをタップした時の処理(現在は選択したセル番号のみを記憶しております。) 25 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath ) { 26 if checkArray.containsObject(indexPath){ //チェックが既に入っているか 27 checkArray.removeObject(indexPath) 28 } 29 }else{ 30 if selecting == true{ //現在は選択状態かどうかの判定 31 checkArray.addObject(indexPath) 32 //ここで選択したセルの中に入っている画像の情報をSelectphotoAssetに確保したい 33 } 34 } 35 collectionView.reloadData() 36 } 37//画像を表示する処理 38 var checkBtnView:UIImageView! 39 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell 40 { 41 //コレクションビューから識別子「PhotoCell」のセルを取得する。 42 let cell = collectionView.dequeueReusableCellWithReuseIdentifier("PhotoCell", forIndexPath: indexPath) as UICollectionViewCell 43 44 let asset = photoAssets[indexPath.row] 45 let imageView = cell.viewWithTag(1) as! UIImageView //iphone内の画像 46 let imageView2 = cell.viewWithTag(2) as! UIImageView //チェックマーク 47 let manager: PHImageManager = PHImageManager() 48 manager.requestImageForAsset(asset, 49 targetSize: imageView.frame.size, 50 contentMode: .AspectFill, 51 options: nil) { (image, info) -> Void in 52 imageView.image = image 53 54 if self.checkArray.containsObject(indexPath){ 55 //チェックマークの表示 56 imageView2.hidden = false 57 }else{ 58 //チェックマークの非表示 59 imageView2.hidden = true 60 } 61 } 62 return cell 63 } 64
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/07/24 23:50