5

我一直在使用iOS 6.x中的ALAssetsLibraryChangedNotification(目前爲6.0.1),並且我得到的結果與我的意見相反期望收到我的用戶信息,基於我從文檔中瞭解的內容。ios 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 *鍵的已接收到的數據。有任何想法嗎?我注意到,即使Apple's sample code聽這個通知,甚至沒有使用密鑰,而是重新枚舉所有資產,無論具體的密鑰(它們聞起來像他們不相信通知信息)

回答

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該組的通知。

+2

謝謝。在閱讀http://developer.apple.com/library/ios/#documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/Reference/Reference.html#//apple_ref/doc/constant_group/Notification_Keys我錯誤地認爲我能夠得到一個受影響資產的URL列表。似乎我們只會得到資產清單,它們是*修改*的時候,以及插入或刪除時,只有相關資產*組的通知*,這使得此通知不如我原先希望的那樣有用,特別是因爲此刻我只處理一個小組。 –

+1

是的,根據文檔:「插入或刪除ALAssets是通過使包含ALAssetGroups無效來標識的。」這是非常不幸的,看起來你必須重新加載整個組(例如相機膠捲),如果資產被添加或刪除。 – akaru