PHPhotoLibrary更改未调用的观察者

klc*_*r89 6 objective-c ios photosframework phphotolibrary

我似乎有一个随机的问题,我不知道为什么会发生.我似乎无法让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];
}
Run Code Online (Sandbox Code Playgroud)

mat*_*att 8

我认为你的测试方式有问题.这对我来说可以.这就是我做的.

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

#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
Run Code Online (Sandbox Code Playgroud)
  • 我在模拟器中运行应用程序.要求授权.我授权.在Xcode运行的模拟器后面,我在控制台中看到"Here" - 这是预料之中的,因为我们会在授权后使库"生效"时收到更改通知.这正是观察者的行为方式.

  • 仍然在模拟器中,我按下Shift-Command-H去跳板.我切换到照片应用并删除照片.

  • 在模拟器中,我按两次Shift-Command-H去了应用程序切换器.

  • 在模拟器中,我单击仍在运行的测试应用程序返回到它.在Xcode的模拟器后面,我在控制台中看到"Here",因为我们外出时,一张照片被删除了.同样,这正是观察者的行为方式.