Swift Gallery GridView Sample

iphoneのギャラリー内の一覧を<font color="PaleVioletRed">インスタグラム風</font>にGridViewへ最新日付け順に表示しクリック選択にて拡大表示する。
![スクリーンショット 2018-03-06 2.17.27.png](https://qiita-image-store.s3.amazonaws.com/0/101462/38dad436-8746-a902-45e7-f226911a3cf0.png)
![スクリーンショット 2018-03-06 11.22.15.png](https://qiita-image-store.s3.amazonaws.com/0/101462/9bf254ca-5dcd-1834-5650-655d4965470a.png)

![スクリーンショット 2018-03-06 11.20.40.png](https://qiita-image-store.s3.amazonaws.com/0/101462/0c995b13-bece-ad72-1798-c105f5ac3b52.png)	
	
	import UIKit
	import Photos
	class ViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
    
    var imageArray = [UIImage]()
    
    var photoAssets = [PHAsset]()
    // PHAssetのリスト変数
    var photoAssets2: Array<PHAsset> = Array<PHAsset>()
    
    var photoUrl = [NSString]()

    var refreshControl: UIRefreshControl = UIRefreshControl()

    @IBOutlet weak var myImageView: UIImageView!
    
    @IBOutlet weak var colViewCell: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //画面サイズ取得
        let myBoundSize: CGSize = UIScreen.mainScreen().bounds.size
        let myBoundSizeStr: NSString = "Bounds width: \(myBoundSize.width) height: \(myBoundSize.height)"
        
        print (myBoundSizeStr)
        
        getAllPhotosInfo()
       
        grabPhotos()
        colViewCell.delegate = self
        colViewCell.dataSource = self
        
    
    }
    
 	//以下にfunc grapPhoto()挿入
 	//collectionViewの設定挿入
 	//cropImageToSquare等設定挿入
    
	}
Gallery内画像を配列へ取得

    func grabPhotos(){
        
        photoAssets = []
        let imgManager = PHImageManager.defaultManager()
        
        let requestOptions = PHImageRequestOptions()
        requestOptions.synchronous = true
        requestOptions.deliveryMode = .HighQualityFormat
        
        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
        
        if let fetchResult : PHFetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions){
            
            if fetchResult.count > 0 {
                
                for i in 0..<fetchResult.count{
                    //print(fetchResult.indexOfObject(imgManager))
                    imgManager.requestImageForAsset(fetchResult.objectAtIndex(i) as! PHAsset, targetSize: CGSize(width: 200, height: 200), contentMode: .AspectFill, options: requestOptions, resultHandler:
                        {
                        image, error in
                        
                        self.imageArray.append(image!)
                        self.photoAssets.append(fetchResult.objectAtIndex(i) as! PHAsset)
                        
                        //self.photoUrl.append(PHAsset.defaultRepresentation().url())
                    })
                    
                    
                    
                }
                
            }
            
        }
        else{
            print("You got no photos!")
            self.colViewCell.reloadData()
            
        }
        
    }

collectionViewの設定
cellが選択されたときcropImageToSquareにて正方形に切取り拡大表示する


	func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageArray.count
    }    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell: ColViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! ColViewCell
        
        //let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
        cell.imgCell.image = imageArray[indexPath.row]
        
        return cell
    }
    
    //cellが選択された場合
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        let manager: PHImageManager = PHImageManager()
        let asset = photoAssets[indexPath.row]
        print("select photoAssets")
        print(asset)
        manager.requestImageForAsset(asset,
                                     targetSize: PHImageManagerMaximumSize,
                                     contentMode: PHImageContentMode.AspectFill,
                                     options: nil) { (image, info) in
                                        // imageにUIImageが渡ってきます
                                        self.myImageView.image = self.cropImageToSquare(image!)
        }
        print("Cell \(asset) selected")
        
    }

	
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        //４列表示で１ドット分隙間に設定
        let width: CGFloat = view.frame.width / 4 - 1
        let height: CGFloat = width
        return CGSize(width: width, height: height)
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
        return 0
    }
cropImageToSquareの設定
	
    func cropImageToSquare(image: UIImage) -> UIImage? {
        if image.size.width > image.size.height {
            // 横長
            let cropCGImageRef = CGImageCreateWithImageInRect(image.CGImage, CGRectMake(image.size.width/2 - image.size.height/2, 0, image.size.height, image.size.height))
            
            return UIImage(CGImage: cropCGImageRef!)
        } else if image.size.width < image.size.height {
            // 縦長
            let cropCGImageRef = CGImageCreateWithImageInRect(image.CGImage, CGRectMake(0, 0, image.size.width, image.size.width))
            
            return UIImage(CGImage: cropCGImageRef!)
        } else {
            return image
        }
    }