title: How to name a photo when saving via iOS swift photos framework
body: <p>I am using <code>Xcode 6.3</code> with swift (I think version 1.2). My app downloads photos from a custom server on the web. Then I want it to save the photo with a given file name to the custom photo album, which was has created via the Photos Framework. I need to be able to control the photo/filename because I keep track of which photos I have downloaded in a database and later need to view them by name.</p>
  
  <p>I created a custom PhotoAlbum class that I am trying to wrap all of the logic for interacting with the photo albums in. Once I download a photo I can call the following code and it works as far as creating the album and saving the photo, but as you will notice in the <code>savePhoto()</code> method I don't know where/how to use the photoName variable to name the photo when saving. Eventually, I will also need to know how to retrieve the photo by name as well in the <code>loadPhoto()</code> method. </p>
  
  <p>Thanks for any help/advice you can give me.</p>
  
  <pre><code>let myAlbum = PhotoAlbum("MyAlbum") 
  
  myAlbum.savePhoto(downloadedPhoto, 12)
  
  class PhotoAlbum {
  
  var albumName: String = ""
  var albumExists: Bool = false
  var assetCollection: PHAssetCollection!
  var photosAsset: PHFetchResult!
  
  init(albumName: String) {
      self.albumName = albumName
      self.initializeAlbum()
  }
  
  private func initializeAlbum() {
  
      let fetchOptions = PHFetchOptions()
      fetchOptions.predicate = NSPredicate(format: "title = %@", self.albumName)
      let collection: PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .Any, options: fetchOptions)
  
      if let firstObject: AnyObject = collection.firstObject {
          self.albumExists = true
          self.assetCollection = collection.firstObject as! PHAssetCollection
          if let ac = self.assetCollection {
              self.photosAsset = PHAsset.fetchAssetsInAssetCollection(self.assetCollection, options: nil)
          }
          println("\(self.albumName) album exists")
      }
      else {
          var albumPlaceHolder: PHObjectPlaceholder!
  
          PHPhotoLibrary.sharedPhotoLibrary().performChanges({
  
              let request = PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle(self.albumName)
              albumPlaceHolder = request.placeholderForCreatedAssetCollection
  
          }, completionHandler: { (success: Bool, error: NSError!) in
  
              self.albumExists = success
              if success {
                  let collection = PHAssetCollection.fetchAssetCollectionsWithLocalIdentifiers([albumPlaceHolder.localIdentifier], options: nil)
                  self.assetCollection = collection.firstObject as! PHAssetCollection
                  if let ac = self.assetCollection {
                      self.photosAsset = PHAsset.fetchAssetsInAssetCollection(self.assetCollection, options: nil)
                  }
                  println("\(self.albumName) album made")
              }
              else {
                  println("Failed to create \(self.albumName) Album!!!!!!")
              }
  
          })
      }
  
  }
  
  
  func savePhoto(image: UIImage, photoID: Int) {
      let photoName = String(photoID)+".jpg"
  
      PHPhotoLibrary.sharedPhotoLibrary().performChanges({
          let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
          let assetPlaceholder = assetRequest.placeholderForCreatedAsset
          let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection, assets: self.photosAsset)
          albumChangeRequest.addAssets([assetPlaceholder])
          }, completionHandler: { (success: Bool, error: NSError!) in
  
              print(success ? "image added" : "failed to add image")
  
      })
  }
  
  func loadPhoto(photoID: Int64) -&gt; UIImage {
      let photoName = String(photoID)+".jpg"
  
      return UIImage(named: "no_photo")!
  }
  
  func renameAlbum(newAlbumName: String) {
      // need to create the new album and move all photos to that album then remove the old album
  
  }
  
  func reloadPhotos() {
      if let ac = self.assetCollection {
          self.photosAsset = PHAsset.fetchAssetsInAssetCollection(self.assetCollection, options: nil)
      }
  }
  </code></pre>