首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未调用PHPhotoLibrary更改观察器

未调用PHPhotoLibrary更改观察器
EN

Stack Overflow用户
提问于 2015-06-09 11:38:40
回答 1查看 9.8K关注 0票数 7

我似乎有一个随机的问题,我不知道为什么会发生。我似乎无法让观察者调用photoLibraryDidChange:(PHChange *)changeInstance。我做了几个空白项目,都在演示这个问题,更改观察器有时会在应用程序初始安装时被调用,但在我在Photos应用程序中执行更改后永远不会调用。我也重置了模拟器,但没有用。如果能提供任何帮助,我将不胜感激。

代码:

代码语言:javascript
复制
#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];
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-09 12:14:57

我认为你的测试方式有问题。它对我来说很好。这是我所做的。

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

代码语言:javascript
复制
#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正在运行,我在控制台中看到了"Here“-这是意料之中的,因为当库在授权后”活过来“时,我们会收到一个更改通知。这正是观察者应该表现出来的。
  • 还在模拟器里,我按下Shift-

-H去跳板。我切换到照片应用程序并删除了一张照片。在模拟器中,我按了两下Shift--H组合键切换应用程序。在模拟器中,我点击了仍在运行的测试应用程序,返回到它。

  • 。在Xcode中的模拟器后面,我在控制台中看到了"Here“,因为当我们外出时,一张照片被删除了。再说一次,这正是观察者应该表现的。
票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30722590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档