我似乎遇到了一个随机问题,我不知道为什么会发生。我似乎无法让photoLibraryDidChange:(PHChange *)changeInstance
观察者调用。我做了几个空白项目,所有项目都在演示这个问题,有时会在初始应用程序安装时调用更改观察者,但在我在照片应用程序中执行更改后从未调用过。我也重置了模拟器无济于事。我将不胜感激提供的任何帮助。
代码:
#import <UIKit/UIKit.h>
#import <Photos/Photos.h>
@interface ViewController : UIViewController <PHPhotoLibraryChangeObserver>
@end
- (void)viewDidLoad
{
[super viewDidLoad];
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status)
{
if (status == PHAuthorizationStatusAuthorized)
{
[PHPhotoLibrary.sharedPhotoLibrary registerChangeObserver:self];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),^
{
[self setup];
});
}
}];
}
- (void)setup
{
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc]init];
fetchOptions.wantsIncrementalChangeDetails = YES;
PHFetchResult *smartAlbumsFetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAny options:fetchOptions];
for (PHAssetCollection *sub in smartAlbumsFetchResult)
{
PHFetchResult *fetch = [PHAsset fetchAssetsInAssetCollection:sub options:fetchOptions];
}
}
- (void)photoLibraryDidChange:(PHChange *)changeInstance
{
NSLog(@"Not called");
}
- (void)dealloc
{
[PHPhotoLibrary.sharedPhotoLibrary unregisterChangeObserver:self];
}