リモートサーバーで管理されている画像のアルバムがあります。私はアルバムをダウンロードして写真のカスタムアルバムに保存するオプションをユーザーに与えたいと思います。しかし、アルバムは動的なので(写真が追加されると)、ユーザーは複数回ダウンロードできます。私は同じ写真を複数回、新しいものだけをダウンロードしたくない。写真に画像があるか確認してください
写真アプリに画像を保存するときにメタデータ(一意のID)を関連付けることはできますか?その画像が既に存在するかどうかを確認します。
写真フレームワークを使用してカスタムアルバムを作成し、写真を保存しています。
編集:ここでは、カスタムアルバムの作成や写真
/** Returns the first album from the photos app with the specified name. */
static func getAlbumWithName(name: String, completion: (album: PHAssetCollection?) -> Void) {
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "localizedTitle = %@", name)
let fetchResult = PHAssetCollection.fetchAssetCollectionsWithType(PHAssetCollectionType.Album, subtype: PHAssetCollectionSubtype.Any, options: fetchOptions)
if fetchResult.count > 0 {
guard let album = fetchResult.firstObject as? PHAssetCollection else {return}
completion(album: album)
} else {
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle(name)
}, completionHandler: { (result, error) in
if result {
FileUtils.getAlbumWithName(name, completion: completion)
} else {
completion(album: nil)
}
})
}
}
/** Adds an image to the specified photos app album */
private static func addImage(image: UIImage, toAlbum album: PHAssetCollection, completion: ((status: Bool) -> Void)?) {
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
let assetPlaceholder = assetRequest.placeholderForCreatedAsset
let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: album)
albumChangeRequest?.addAssets([assetPlaceholder!])
}) { (status, error) in
completion?(status: status)
}
}
このカスタムアルバムに写真をダウンロードして保存する方法のコードを表示するために質問を編集すると役立つ場合があります –