title: How to load the Photo Library into UICollectionView? Swift
body: <p><strong>What I'm trying to accomplish:</strong> For this project, I will mention only two source files: (1) RPPhotoLibrary.swift - A UICollectionViewController and (2) PhotoThumbnail.swift: - A UICollectionViewCell. I want to load the images (and videos) from the user's photo library (preferably <em>all of them</em>) and present them into a UICollectionViewCell for each cell in the UICollectionViewController. For some reason, I am able to load photos but <em>am not</em> able to load <em>all the photos</em>. This is my code for the first source file: RPPhotoLibrary.swift:</p>
  <pre><code>import UIKit
  import Photos
  
  private let reuseIdentifier = &quot;PhotoCell&quot;
  
  class RPPhotoLibrary: UICollectionViewController,     UIImagePickerControllerDelegate {
  
  
  // By default make locating album false
  var assetCollection: PHAssetCollection!
  var photosAsset: PHFetchResult!
  var assetThumbnailSize: CGSize!
  
  @IBAction func cancelButton(sender: AnyObject) {
      self.dismissViewControllerAnimated(false, completion: nil)
  }
  
  // =================== THIS BUTTON TAKES A PHOTO =====================================================
  @IBAction func captureMoment(sender: AnyObject) {
      let imagePicker = UIImagePickerController()
      
      if (UIImagePickerController.isSourceTypeAvailable(.Camera)) {
          
          if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil {
              
              imagePicker.allowsEditing = true
              imagePicker.sourceType = .Camera
              imagePicker.cameraCaptureMode = .Photo
              presentViewController(imagePicker, animated: false, completion: {} )
              
          } else {
              // postAlert(&quot;Rear camera does not exist&quot;, message: &quot;RedPlanet cannot access the camera&quot;)
              var alert = UIAlertView(title:  &quot;Your phone doesn't have a rear camera!&quot;,
                  message: &quot;RedPlanet cannot access the camera.&quot;,
                  delegate: self,
                  cancelButtonTitle: &quot;ok&quot;)
              alert.show()
          }
          
      } else {
          // postAlert(&quot;Camera not accessible&quot;, message: &quot;RedPlanet cannot access the camera&quot;)
          var alert = UIAlertView(title:  &quot;Cannot access camera.&quot;,
              message: &quot;RedPlanet cannot access the camera.&quot;,
              delegate: self,
              cancelButtonTitle: &quot;ok&quot;)
          alert.show()
      }
  }
  
  @IBOutlet weak var thumbNail: UIImageView!
  
  override func viewDidLoad() {
      super.viewDidLoad()
      
      let fetchOptions = PHFetchOptions()
  
      let collection:PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.Moment, subtype: .Any, options: fetchOptions)
      
      if let first_Obj:AnyObject = collection.firstObject{
          //found the album
          self.assetCollection = first_Obj as! PHAssetCollection
      }
  }
  
  override func viewWillAppear(animated: Bool) {
      // Get size of the collectionView cell for thumbnail image
      if let layout = self.collectionView!.collectionViewLayout as? UICollectionViewFlowLayout{
          let cellSize = layout.itemSize
          
          self.assetThumbnailSize = CGSizeMake(cellSize.width, cellSize.height)
      }
      
      //fetch the photos from collection
      self.photosAsset = PHAsset.fetchAssetsInAssetCollection(self.assetCollection, options: nil)
      
      self.collectionView!.reloadData()
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
  }
  
  
  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
      if(segue.identifier == &quot;selectedPhoto(s)&quot;){
         
          if let controller: SelectedPhoto = segue.destinationViewController as? SelectedPhoto{
              
              if let cell = sender as? PhotoThumbnail {
                  if let indexPath: NSIndexPath = self.collectionView!.indexPathForCell(cell){
                      controller.index = indexPath.item
                      controller.photosAsset = self.photosAsset
                      controller.assetCollection = self.assetCollection
                  }
              }
              
          }
      }
  }
  
  
  // MARK: UICollectionViewDataSource
  
  override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -&gt; Int {
      // #warning Incomplete implementation, return the number of sections
      return 1
  }
  
  
  override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
      // #warning Incomplete implementation, return the number of items
      var count: Int = 0
      
      if(self.photosAsset != nil){
          count = self.photosAsset.count
      }
      
      return count;
  }
  
  
  override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -&gt; UICollectionViewCell {
      let cell: PhotoThumbnail = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! PhotoThumbnail
      
      //Modify the cell
      let asset: PHAsset = self.photosAsset[indexPath.item] as! PHAsset
      
      PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: self.assetThumbnailSize, contentMode: .AspectFill, options: nil, resultHandler: {(result, info)in
          if let image = result {
              cell.setThumbnailImage(image)
          }
      })
      
      return cell
  }
  
  // MARK: - UICollectionViewDelegateFlowLayout methods
  func collectionView(collectinView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -&gt; CGFloat {
      return 4
  }
  
  func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -&gt; CGFloat {
      return 1
  }
  
  // UIImagePickerControllerDelegate Methods
  func imagePickerControllerDidCancel(picker: UIImagePickerController){
      picker.dismissViewControllerAnimated(true, completion: nil)
  }
  
  // MARK: UICollectionViewDelegate
  
  /*
  // Uncomment this method to specify if the specified item should be selected
  override func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -&gt; Bool {
      return true
  }
  */
  
  
  // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
  override func collectionView(collectionView: UICollectionView, shouldShowMenuForItemAtIndexPath indexPath: NSIndexPath) -&gt; Bool {
      return false
  }
  
  override func collectionView(collectionView: UICollectionView, canPerformAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -&gt; Bool {
      return false
  }
  
  override func collectionView(collectionView: UICollectionView, performAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) {
      
      self.dismissViewControllerAnimated(false, completion: nil)
  
  }
  </code></pre>
  <p>}</p>
  <p>Here's my code for the second source file (2): PhotoThumbnail.swift:</p>
  <pre><code>import UIKit
  
  
  class PhotoThumbnail: UICollectionViewCell {
  
  @IBOutlet weak var thumbNail: UIImageView!
  
  func setThumbnailImage(thumbNailImage: UIImage) {
      self.thumbNail.image = thumbNailImage
  }
  
  }
  </code></pre>
  <p><strong>The Problem:</strong> For some reason, I am unable to load <em>all</em> the images from the Photos Library with 8 columns and I want to load 4 columns of each photo from the photo library. If anyone could help me out or even give me tutorials for something likes this, that would be greatly appreciated!</p>