未调用 PHPhotoLibrary 更改观察器

PHPhotoLibrary change observer not called

我似乎遇到了一个我不知道为什么会发生的随机问题。我似乎无法让 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];
}

我认为您的测试方式有问题。这对我来说可以。这是我所做的。

这是我的一个视图控制器的全部代码:

#import <UIKit/UIKit.h>
@import Photos;
#import "ViewController.h"

@interface ViewController() <PHPhotoLibraryChangeObserver>
@end
@implementation ViewController : UIViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
         if (status == PHAuthorizationStatusAuthorized) {
             [PHPhotoLibrary.sharedPhotoLibrary registerChangeObserver:self];
         }
     }];
}
- (void)photoLibraryDidChange:(PHChange *)changeInstance
{
    NSLog(@"Here");
}
@end
  • 我 运行 模拟器中的应用程序。请求授权。我授权。在模拟器后面,Xcode 是 运行ning,我在控制台中看到 "Here" - 这是预料之中的,因为当库 "comes to life" 授权后,我们会收到更改通知。这正是观察者的行为方式。

  • 仍然在模拟器中,我按 Shift-Command-H 转到跳板。我切换到“照片”应用并删除了一张照片。

  • 在模拟器中,我按了两次 Shift-Command-H 以进入应用程序切换器。

  • 在Simulator中,我点击still-运行ning test app来return。在 Xcode 中的模拟器后面,我在控制台中看到 "Here",因为我们不在的时候,一张照片被删除了。同样,这正是观察者的行为方式。