我使用PHPhotoLibrary
检索一些照片收藏是这样的:PHChange changeDetailsForFetchResult - 总是返回零
_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];
}
});
}
N嗷嗷如果我打开照片应用添加/修改/从“所有照片” -SmartAlbum,更改观察者被调用,但
[changeInstance changeDetailsForFetchResult:_smartAlbumsFetchResult]
只返回nil
通话删除照片,即使PHChange对象包含insertedObjectIDs
和changedObjectIDs
。使用iDevice的Home和Lock按钮进行屏幕截图时也是如此。
有点迟了,但是您是否试图用非零选项和wantsIncrementalChangeDetails = YES调用[PHAssetCollection fetchAssetCollectionsWithType:...]? – Ivan