我想将资产从一张专辑移到另一张专辑。但是我只能找到有关创建和删除资产的信息。我想移动而不是创建一个新副本并删除旧副本的原因是,我想保留资产的localIdentifier
之类的数据位。如果我在另一个相册中重新创建资产,则会为其分配新的localIdentifier
,对吗?
但是似乎不可能。因此,我至少采取了复制资产的措施。但这也给我带来麻烦。
这是我的代码。
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)
}
}
})
}
该应用程序在
let assetPlaceholder = assetChangeRequest.placeholderForCreatedAsset!
行崩溃,无法找到nil
的placeholderForCreatedAsset
值。这意味着PHAssetChangeRequest(forAsset: asset)
仅可用于更改资产的元数据,而不能复制它。还有其他方法吗?任何解决方法或破解方法?我将非常感谢您的帮助。
最佳答案
就像将资产从一个相册复制到另一个相册一样:在iOS照片库中,所有原始照片/视频都位于“所有照片” /“相机胶卷”中。相册仅包含对“所有照片/相机胶卷”中原始照片/录像的引用。这意味着您可以将一张照片分配给n个相册。要管理这些任务,请查看PHAssetCollectionChangeRequest
类。要在相册中添加一张或多张照片/视频,请使用addAssets:
,使用removeAssets:
方法从相册中删除资产。
关于ios - 将PHAsset从一张专辑移动/复制到另一张专辑,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35241409/