5

我一直在使用 iOS 6.x(目前特别是 6.0.1)中的 ALAssetsLibraryChangedNotification,根据我从文档中了解到的信息,我得到的结果与我期望在我的用户信息中收到的结果相反.

这是我的事件注册代码:

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(assetsLibraryDidChange:) name:ALAssetsLibraryChangedNotification object:_library];

为了测试,我进入我的照片库,删除一些项目,添加一些项目。

这是我的处理程序。

    - (void)assetsLibraryDidChange:(NSNotification *)note
{
  NSDictionary* info = [note userInfo];
  NSLog(@"assetsLibraryDidChange calling syncPhotoInfoFromAssets, userInfo %@", info);
  //   If the user information dictionary is nil, reload all assets and asset groups.
  if(note.userInfo==nil) {
    [self syncPhotoInfoFromAssets];
    return;
  }

  //   If the user information dictionary an empty dictionary, there is no need to reload assets and asset groups.
  if(note.userInfo.count == 0) {
    return;
  }

 // If the user information dictionary is not empty, reload the effected assets and asset groups. For the keys used, see “Notification Keys.”
  NSSet *updatedAssets = [info objectForKey:ALAssetLibraryUpdatedAssetsKey];
  NSSet *updatedAssetGroup = [info objectForKey:ALAssetLibraryUpdatedAssetGroupsKey];
  NSSet *deletedAssetGroup = [info objectForKey:ALAssetLibraryDeletedAssetGroupsKey];
  NSSet *insertedAssetGroup = [info objectForKey:ALAssetLibraryInsertedAssetGroupsKey];

  NSLog(@"updated assets:%@", updatedAssets);
  NSLog(@"updated asset group:%@", updatedAssetGroup);
  NSLog(@"deleted asset group:%@", deletedAssetGroup);
  NSLog(@"inserted asset group:%@", insertedAssetGroup);
  //further processing here
}

我的输出:

   ALAssetLibraryUpdatedAssetGroupsKey = "{(\n    assets-library://group/?id=736B6346-6DA2-4E43-8830-9C263B2D29ED\n)}";
    ALAssetLibraryUpdatedAssetsKey = "{(\n    assets-library://asset/asset.JPG?id=A695208B-3546-4CCA-B539-B1D132A209B3&ext=JPG\n)}";
}
2013-01-06 22:50:45.738 Olesi[25468:3613] updated assets:{(
    assets-library://asset/asset.JPG?id=A695208B-3546-4CCA-B539-B1D132A209B3&ext=JPG
)}
2013-01-06 22:50:45.738 Olesi[25468:3613] updated asset group:{(
    assets-library://group/?id=736B6346-6DA2-4E43-8830-9C263B2D29ED
)}
2013-01-06 22:50:45.739 Olesi[25468:3613] deleted asset group:(null)
2013-01-06 22:51:06.658 Olesi[25468:3613] inserted asset group:(null)

删除并插入专辑后,我希望在 ALAssetLibraryDeletedAssetGroupsKey 和 ALAssetLibraryInsertedAssetGroupsKey 中都收到数据,而在 ALAssetLibraryUpdatedAsset*Key 中都没有。有任何想法吗?我注意到,即使是Apple 监听此通知的示例代码甚至都没有使用密钥,而是重新枚举所有资产,而不管具体密钥如何(闻起来就像他们不信任通知信息)

4

1 回答 1

6

如果您没有对要删除的组的引用,则操作系统不会让您知道。

我使用以下代码得到正确的行为:

#import "ROBKViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>

@interface ROBKViewController ()

@property (nonatomic, strong) ALAssetsLibrary *assetsLibrary;
@property (nonatomic, strong) ALAssetsGroup *currentAssetGroup;

- (void) handleAssetChangedNotifiation:(NSNotification *)notification;

@end

@implementation ROBKViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {
        _assetsLibrary = [ALAssetsLibrary new];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAssetChangedNotifiation:) name:ALAssetsLibraryChangedNotification object:_assetsLibrary];
    }

    return self;
}

- (void) dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:ALAssetsLibraryChangedNotification object:nil];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    } failureBlock:^(NSError *error) {
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Notification handlers

- (void) handleAssetChangedNotifiation:(NSNotification *)notification
{
    NSLog(@"notification: %@", notification);

    if ([notification userInfo]) {
        NSSet *insertedGroupURLs = [[notification userInfo] objectForKey:ALAssetLibraryInsertedAssetGroupsKey];
        NSURL *assetURL = [insertedGroupURLs anyObject];
        if (assetURL) {
            [self.assetsLibrary groupForURL:assetURL resultBlock:^(ALAssetsGroup *group) {
                self.currentAssetGroup = group;
            } failureBlock:^(NSError *error) {

            }];
        }
    }

}

@end

请注意,我只收到分配给 self.currentAssetGroup 的组的通知。

于 2013-01-07T14:31:58.230 回答