我似乎有一个随机的问题,我不知道为什么会发生。我似乎无法让观察者调用photoLibraryDidChange:(PHChange *)changeInstance。我做了几个空白项目,都在演示这个问题,更改观察器有时会在应用程序初始安装时被调用,但在我在Photos应用程序中执行更改后永远不会调用。我也重置了模拟器,但没有用。如果能提供任何帮助,我将不胜感激。
代码:
#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];
}发布于 2015-06-09 12:14:57
我认为你的测试方式有问题。它对我来说很好。这是我所做的。
这是我的一个视图控制器的完整代码:
#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-H去跳板。我切换到照片应用程序并删除了一张照片。在模拟器中,我按了两下Shift--H组合键切换应用程序。在模拟器中,我点击了仍在运行的测试应用程序,返回到它。
https://stackoverflow.com/questions/30722590
复制相似问题