2016-02-06 42 views
0

我想將資源從一個相冊移到另一個相冊。但我只能找到有關創建和刪除資產的信息。我想移動而不是創建新副本並刪除舊副本的原因是,我想保留資產的各個位,如資產的localIdentifier。如果我在另一個相冊中重新創建資源,它將被分配一個新的localIdentifier,對嗎?將PHAsset從一個相冊移動/複製到另一個

但似乎這是不可能的。所以我採取至少複製資產。但它也給我帶來麻煩。

這是我的代碼。

func copyAsset() { 
    if let assets = getAssetsFromCollection(collectionTitle: "Old") { 
     if getAssetColection("New") == nil { 
      createCollection("New") { collection in 
       if let collection = collection { 
        print("Collection created") 
        self.copyAssetsToCollection(assets, toCollection: collection) 
       } 
      } 
     } 
    } 
} 

/** 
Copy assets to album. 

- parameter assets:  Array of assets to copy. 
- parameter collection: The collection where the assets need to be copied. 
*/ 
func copyAssetsToCollection(assets: [PHAsset], toCollection collection: PHAssetCollection) { 
    PHPhotoLibrary.sharedPhotoLibrary().performChanges({ 
     let asset = assets.first! 
     print(asset.localIdentifier) 
     let assetChangeRequest = PHAssetChangeRequest(forAsset: asset) 
     let assetPlaceholder = assetChangeRequest.placeholderForCreatedAsset! 

     let collectionChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: collection)! 
     collectionChangeRequest.addAssets([assetPlaceholder]) 
    }, completionHandler: { success, error in 
     if success { 
      print("Photo copied to collection") 
     } else { 
      if let error = error { 
       print("Error adding photos to collection: \(error.code) - \(error.localizedDescription)") 
      } 
     } 
    }) 
} 

/** 
Retrieve the album for the given title. 

- parameter title: Album title. 

- returns: Album if it exists. nil if it doesn't. 
*/ 
func getAssetColection(title: String) -> PHAssetCollection? { 
    let fetchOptions = PHFetchOptions() 
    fetchOptions.predicate = NSPredicate(format: "title ==[cd] %@", title) 
    if let collection = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .AlbumRegular, options: fetchOptions).firstObject { 
     return collection as? PHAssetCollection 
    } 
    return nil 
} 

/** 
Get assets from a given album. 

- parameter title: Album title. 

- returns: An array of assets if they exist. nil if they don't. 
*/ 
func getAssetsFromCollection(collectionTitle title: String) -> [PHAsset]? { 
    var assets = [PHAsset]() 
    if let collection = getAssetColection(title) { 
     let fetchOptions = PHFetchOptions() 
     fetchOptions.predicate = NSPredicate(format: "mediaType == %d", PHAssetMediaType.Image.rawValue) 
     let fetchResult = PHAsset.fetchAssetsInAssetCollection(collection, options: fetchOptions) 
     fetchResult.enumerateObjectsUsingBlock { object, index, stop in 
      if let asset = object as? PHAsset { 
       assets.append(asset) 
      } 
     } 
     return assets 
    } 
    return nil 
} 

/** 
Create an album in Photos app. 

- parameter title:  Album title. 
- parameter completion: Album if successfully created. nil if it fails. 
*/ 
func createCollection(title: String, completion: (collection: PHAssetCollection?) ->()) { 
    var albumPlaceholder: PHObjectPlaceholder! 
    PHPhotoLibrary.sharedPhotoLibrary().performChanges({ 
     let request = PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle(title) 
     albumPlaceholder = request.placeholderForCreatedAssetCollection 
    }, completionHandler: { success, error in 
     if success { 
      let fetchResult = PHAssetCollection.fetchAssetCollectionsWithLocalIdentifiers([albumPlaceholder.localIdentifier], options: nil) 
      completion(collection: fetchResult.firstObject as? PHAssetCollection) 
     } else { 
      if let error = error { 
       print("Failed to create album: \(title) in Photos app: \(error.code) - \(error.localizedDescription)") 
       completion(collection: nil) 
      } 
     } 
    }) 
} 

在尋找用於placeholderForCreatedAssetnil值線let assetPlaceholder = assetChangeRequest.placeholderForCreatedAsset!應用崩潰。這意味着PHAssetChangeRequest(forAsset: asset)只能用於更改資產的元數據,而不能複製它。

有沒有其他方法可以做到這一點?任何解決方法或破解?我會很感激任何幫助。

回答

1

沒有什麼比將資源從一張專輯複製到另一張:在iOS照片庫中,所有原始照片/視頻均位於「所有照片」/「相機膠捲」中。相冊僅包含位於「所有照片/相機膠捲」中的原始照片/視頻的引用。這意味着您可以將一張照片分配給n張專輯。要管理這些作業,請查看PHAssetCollectionChangeRequest課程。要在相冊中添加一個或多個照片/視頻,請使用addAssets:,以使用removeAssets:方法從相冊中移除資源。

相關問題