ich die PHPhotoLibrary
bin mit mehreren Fotosammlungen wie folgt abzurufen:PHChange changeDetailsForFetchResult - gibt immer nil
_smartAlbumsFetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumMyPhotoStream options:nil];
[_smartAlbumsFetchResult enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) {
[self analyzeCollectionInternal:collection];
}];
_userAlbumsFetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:userAlbumsOptions];
[_userAlbumsFetchResult enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) {
[self analyzeCollectionInternal:collection];
}];
Die Methode analyzeCollectionInternal
die PHAssetCollections
zur späteren Verwendung speichert und zeigt den Inhalt der Sammlungen.
- (void)analyzeCollectionInternal:(PHAssetCollection *)collection {
NSLog(@"Album Title %@", collection.localizedTitle);
if (![_collections containsObject:collection]) {
[_collections addObject:collection];
}
[...]
}
Zusätzlich registriert ich eine Veränderung Beobachter an den Fotos Bibliothek wie folgt aus:
[[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self];
Die Klasse, die als Beobachter fungiert implementiert das PHPhotoLibraryChangeObserver
Protokoll wie folgt aus:
- (void)photoLibraryDidChange:(PHChange *)changeInstance {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"PicturesDataSource - photoLibraryDidChange");
NSMutableArray *collectionsToAnalyze = [NSMutableArray new];
NSMutableArray *collectionsToDelete = [NSMutableArray new];
if (_smartAlbumsFetchResult) {
PHFetchResultChangeDetails *smartAlbumChanges = [changeInstance changeDetailsForFetchResult:_smartAlbumsFetchResult];
NSLog(@"PictureDataSource - changeDetailsForFetchResult(_smartAlbumsFetchResult): %@", smartAlbumChanges);
if (smartAlbumChanges) {
_smartAlbumsFetchResult = [smartAlbumChanges fetchResultAfterChanges];
[collectionsToAnalyze addObjectsFromArray:smartAlbumChanges.insertedObjects];
[collectionsToAnalyze addObjectsFromArray:smartAlbumChanges.changedObjects];
[collectionsToDelete addObjectsFromArray:smartAlbumChanges.removedObjects];
}
}
if (_userAlbumsFetchResult) {
PHFetchResultChangeDetails *userAlbumChanges = [changeInstance changeDetailsForFetchResult:_userAlbumsFetchResult];
NSLog(@"PictureDataSource - changeDetailsForFetchResult(_userAlbumsFetchResult): %@", userAlbumChanges);
if (userAlbumChanges) {
_userAlbumsFetchResult = [userAlbumChanges fetchResultAfterChanges];
[collectionsToAnalyze addObjectsFromArray:userAlbumChanges.insertedObjects];
[collectionsToAnalyze addObjectsFromArray:userAlbumChanges.changedObjects];
[collectionsToDelete addObjectsFromArray:userAlbumChanges.removedObjects];
}
}
for (PHAssetCollection *collectionToAnalyze in collectionsToAnalyze) {
[self analyzeCollection:collectionToAnalyze];
}
for (PHAssetCollection *collectionToDelete in collectionsToDelete) {
[self deleteCollection:collectionToDelete];
}
});
}
N ow, wenn ich die Fotos App öffnet Hinzufügen/Ändern/Löschen von Fotos aus dem „Alle Fotos“ -SmartAlbum, die Change-Observer aufgerufen werden, aber der Aufruf von
[changeInstance changeDetailsForFetchResult:_smartAlbumsFetchResult]
liefert nur nil
, auch wenn das Objekt PHChange enthält insertedObjectIDs
und changedObjectIDs
. Gleiches gilt für Screenshots mit dem Home- und Lock-Button des iDevice.
Ein bisschen spät, aber haben Sie versucht, [PHAssetCollection fetchAssetCollectionsWithType: ...] mit Nicht-Nil-Optionen und mit willsIncrementalChangeDetails = YES? – Ivan