title: How to loop through a photo gallery in swift with photos framework
body: <p>I have an app that creates a unique photo gallery for each ticket on my app. I need to figure out how to loop through that gallery so I can upload the images to the server one at a time. I followed these tutorials on youtube. (<a href="https://www.youtube.com/watch?v=ztIBNHOm35E&amp;list=PL0wGKPc4_PKNsEa_QVEQM0J5a2UJ1sNHc" rel="noreferrer">Video</a>) I am currently using Swift as my language. my ultimate goal would be to have something like the following pseudo code.</p>
  
  <pre><code>//I need the following for loop example
  for photo in gallery
  {
      uploadToServer(photo)
  }
  
  //this function works already
  
  func uploadToServer(photo:UIImage)
  {
      callRest
      postPhoto
  }
  </code></pre>
  
  <p>here is my code so far cannot figure out how to loop through. them though.</p>
  
  <pre><code>let fetchOptions = PHFetchOptions()
  fetchOptions.predicate = NSPredicate(format: "title = %@", self.albumName)?
  let collection:PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .Any, options: fetchOptions)
  
  if let first_Obj:AnyObject = collection.firstObject{
      //found the album
      self.assetCollection = collection.firstObject as PHAssetCollection
      self.albumFound = true
  }
  else { albumFound = false }
  var i = collection.count
  self.photoAssets = PHAsset.fetchAssetsInAssetCollection(self.assetCollection, options: nil)
  
  
  self.photoAssets.enumerateObjectsUsingBlock{(object: AnyObject!,
      count: Int,
      stop: UnsafeMutablePointer&lt;ObjCBool&gt;) in
  
      if object is PHAsset{
          let asset = object as PHAsset
          println("Inside  If object is PHAsset, This is number 1")
  
          let imageSize = CGSize(width: asset.pixelWidth,
              height: asset.pixelHeight)
  
          /* For faster performance, and maybe degraded image */
          let options = PHImageRequestOptions()
          options.deliveryMode = .FastFormat
  
          imageManager.requestImageForAsset(asset,
              targetSize: imageSize,
              contentMode: .AspectFill,
              options: options,
              resultHandler: {
                  (image, info)-&gt;Void in
  
                  self.photo = image
  
                  var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
                  var server = defaults.objectForKey("imageServer") as String!
                  var error: NSError? = nil
  
                  var imageData = UIImageJPEGRepresentation(self.photo, 90)
                  var url = NSURL(string: server)
                  var request = NSMutableURLRequest(URL: url!)
                  request.HTTPMethod = "POST"
                  //request.setValue("multipart/form-data", forHTTPHeaderField: "Content-Type")
                  request.HTTPBody = imageData
  
                  var response: NSURLResponse? = nil
  
                  let reply = NSURLConnection.sendSynchronousRequest(request, returningResponse:&amp;response, error:&amp;error)
  
                  let results = NSString(data:reply!, encoding:NSUTF8StringEncoding)
                  println("API Response: \(results)")
  
                  /*
                  /* The image is now available to us */
                  self.sendPhotos(image)
                  println("enum for image, This is number 2")*/
  
  
          })
      }
  }
  </code></pre>