ios 6.0.1 ALAssetsLibraryChangedNotification,试图了解发送的内容

Jon*_*son 5 objective-c nsnotificationcenter assetslibrary alassetslibrary

我一直在使用iOS 6.x中的ALAssetsLibraryChangedNotification(目前特别是6.0.1),我得到的结果与我期望在我的userinfo中收到的内容相反,基于我从文档中理解的内容.

这是我的事件注册代码:

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(assetsLibraryDidChange:) name:ALAssetsLibraryChangedNotification object:_library];
Run Code Online (Sandbox Code Playgroud)

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

这是我的经纪人.

    - (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
}
Run Code Online (Sandbox Code Playgroud)

我的输出:

   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)
Run Code Online (Sandbox Code Playgroud)

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

Kri*_*kel 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
Run Code Online (Sandbox Code Playgroud)

请注意,我只获取分配给self.currentAssetGroup的组的通知.

  • 谢谢.在阅读http://developer.apple.com/library/ios/#documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/Reference/Reference.html#//apple_ref/doc/constant_group/Notification_Keys我误读了我能得到一个任何受影响资产的URL列表.似乎我们只获得一个资产列表,当它们被*修改*时,对于插入或删除,只有他们相关资产*组*的通知,使这个通知没有我原先希望的那么有用,特别是从目前开始我只和一个团体打交道. (2认同)