我一直在使用 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 监听此通知的示例代码甚至都没有使用密钥,而是重新枚举所有资产,而不管具体密钥如何(闻起来就像他们不信任通知信息)