将PHAsset从一个专辑移动/复制到另一个专辑

时间:2016-02-06 13:11:19

标签: ios swift copy phasset photokit

我想将资产从一张专辑转移到另一张专辑。但我只能找到有关创建和删除资产的信息。我想移动而不是创建新副本并删除旧副本的原因是,我想保留资产的level = int(input('Please enter your pokemons level :' )) Attackstat= int(input('Please enter your pokemons special/attack stat : ')) Defensestat= int(input('Please enter your opponents special/defense stat : ')) Attackpower= int(input('Please enter your moves base attack : ')) answer= str('Is your move STAB? : ') STAB= if answer == yes: STAB= 1.5 else: STAB= 1 answer1= int('How effective is your move?') resistance= if answer1== 4: resistance= 4 if answer1== 2: resistance= 2 if answer1== 1: resistance= 1 if answer1== 0.5: resistance= 0.5 if answer1== 0.25: resistance= 0.25 damage= (((2* level/5 +2) * Attackstat * Attackpower/ Defensestat)/50)+2)*STAB*resistance/100 print (damage) 等数据位。如果我在另一个相册中重新创建资源,系统会为其分配一个新的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!的{​​{1}}值。这意味着nil只能用于更改资源的元数据而不能复制它。

还有其他办法吗?任何变通方法或黑客攻击?我真的很感激任何帮助。

1 个答案:

答案 0 :(得分:1)

将资产从一个专辑复制到另一个专辑没有任何意义:在iOS照片库中,所有原始照片/视频都位于"所有照片" /"相机胶卷"。相册仅包含对位于"所有照片/相机胶卷"中的原始照片/视频的引用。这意味着您可以将一张照片分配给n-Albums。要管理这些作业,请查看PHAssetCollectionChangeRequest课程。要在相册中添加一个或多个照片/视频,请使用addAssets:,使用removeAssets:方法从相册中删除资源。