我正在使用PHPhotoLibrary
检索几个这样的照片集:
_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];
}];
该方法analyzeCollectionInternal
保存PHAssetCollections
供以后使用并显示集合的内容。
- (void)analyzeCollectionInternal:(PHAssetCollection *)collection {
NSLog(@"Album Title %@", collection.localizedTitle);
if (![_collections containsObject:collection]) {
[_collections addObject:collection];
}
[...]
}
此外,我向照片库注册了一个更改观察者,如下所示:
[[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self];
充当观察者的类实现了PHPhotoLibraryChangeObserver
这样的协议:
- (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];
}
});
}
现在,如果我打开照片应用程序以从“所有照片”-SmartAlbum 添加/更改/删除照片,则会调用更改观察者,但调用
[changeInstance changeDetailsForFetchResult:_smartAlbumsFetchResult]
仅返回nil
,即使 PHChange 对象包含insertedObjectIDs
and changedObjectIDs
。使用 iDevice 的 Home- 和 Lock-Button 截屏时也是如此。