Adding photo to custom folder using PHPhotoLibrary also adds photo to camera roll

时间:2015-11-18 21:01:28

标签: ios swift camera-roll phphotolibrary

I am using PHPhotoLibrary to create a custom folder and add pictures in it. It works ok, but the picture I am adding to the custom folder is also added to the "camera roll" folder. I found this old thread Add photos in custom album also added in default Album "Camera Roll", where somebody says that it is the normal behaviour, but since the comment is from 3 years ago, I would like to know if in the latests iOS versions there is a way to add the picture only to the custom folder. I have searched for some documentation to confirm this, but no luck.

The code I am using:

class TMPhotoAlbum {

  static let albumName = "Custom Album"
  static let sharedInstance = TMPhotoAlbum()

  var assetCollection: PHAssetCollection!

  init() {

    func fetchAssetCollectionForAlbum() -> PHAssetCollection! {

      let fetchOptions = PHFetchOptions()
      fetchOptions.predicate = NSPredicate(format: "title = %@", TMPhotoAlbum.albumName)
      let collection = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .Any, options: fetchOptions)

      if (collection.firstObject != nil) {
        return collection.firstObject as! PHAssetCollection
      }

      return nil
    }

    if let assetCollection = fetchAssetCollectionForAlbum() {
      self.assetCollection = assetCollection
      return
    }

    PHPhotoLibrary.sharedPhotoLibrary().performChanges({
      PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle(TMPhotoAlbum.albumName)
      }) { success, _ in
        if success {
          self.assetCollection = fetchAssetCollectionForAlbum()
        }
    }
  }

  func saveImage(image: UIImage, completion: (Bool, NSError?) -> Void) {

    if assetCollection == nil {
      return   // If there was an error upstream, skip the save.
    }

    PHPhotoLibrary.sharedPhotoLibrary().performChanges({
      let assetChangeRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
      let assetPlaceholder = assetChangeRequest.placeholderForCreatedAsset
      let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection)
      albumChangeRequest?.addAssets([assetPlaceholder!])
      }, completionHandler: completion )
  }

}

1 个答案:

答案 0 :(得分:0)

它仍然是正常和预期的行为:用户创建的相册中的照片/视频仅引用了#34;相机胶卷"中的原件。这个概念从iOS 5开始就存在,到目前为止还没有改变。

唯一不属于相机胶卷的照片/视频是iTunes同步的照片/视频,共享光电流和使用相机连接套件导入的项目。