gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 03:51:16 27 4
gpt4 key购买 nike

我一直在 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's sample code收听此通知的人甚至不使用 key ,而是重新枚举所有 Assets ,而不考虑特定 key (这听起来像是他们不信任通知信息)

最佳答案

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

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

#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 的组的通知。

关于objective-c - ios 6.0.1 ALAssetsLibraryChangedNotification,试图了解发送的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14191331/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com