5

Ich habe mit ALAssentsLibraryChangedNotification in iOS 6.x (6.0.1 speziell im Moment) gearbeitet, und ich bekomme Ergebnisse sind wider, was ich würde erwarte in meiner Benutzerinfo, basierend auf was ich aus der Dokumentation verstehe.ios 6.0.1 ALAssentsLibraryChangedNotification, versuche zu verstehen, was gesendet wird

Hier ist mein Code für Event-Registrierung:

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

zu testen, ich in meiner Foto-Bibliothek gehen, und einige Elemente löschen, einige Elemente hinzuzufügen.

hier ist mein Handler.

- (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 
} 

meine Ausgabe:

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) 

Nach dem Löschen und ein Album einfügen, erwarte ich, dass sowohl die ALAssetLibraryDeletedAssetGroupsKey und ALAssetLibraryInsertedAssetGroupsKey in Daten erhalten zu haben, und nichts in einem der ALAssetLibraryUpdatedAsset * Key. Irgendwelche Ideen? Ich bemerke, dass selbst Apple's sample code, die diese Benachrichtigung abhört, nicht einmal die Schlüssel verwendet, sondern alle Assets neu aufzählt, unabhängig vom spezifischen Schlüssel (der riecht, als würden sie der Benachrichtigungsinformation nicht trauen)

Antwort

6

If Sie haben keinen Verweis auf die zu löschende Gruppe. Das OS lässt Sie nicht wissen.

ich das richtige Verhalten mit dem folgenden Code:

#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 

Bitte beachte, dass ich die Benachrichtigung für die Gruppe nur erhalten, der self.currentAssetGroup zugewiesen.

+2

Vielen Dank. Beim Lesen von http://developer.apple.com/library/ios/#documentation/AssentsLibrary/Reference/ALAsssetsLibrary_Class/Reference/Reference.html#//apple_ref/doc/constant_group/Notification_Keys habe ich falsch gelesen, dass ich in der Lage wäre, ein Liste der URLs für alle betroffenen Assets. Scheint, dass wir nur eine Liste von Assets bekommen, wenn sie * modifiziert * sind, und für Einfügungen oder Löschungen nur eine Benachrichtigung für ihre zugehörigen Asset * -Gruppen *, was diese Benachrichtigung weniger nützlich macht, als ich ursprünglich gehofft hatte, besonders seit dem Moment Ich habe nur mit einer Gruppe zu tun. –

+1

Ja, laut der Dokumentation: "Eingefügte oder gelöschte ALAssets werden identifiziert, indem die enthaltenen ALAssetGroups ungültig gemacht werden." Dies ist sehr bedauerlich und es scheint, dass Sie die gesamte Gruppe (z. B. die Kamerarolle) neu laden müssen, wenn ein Asset hinzugefügt oder entfernt wurde. – akaru

Verwandte Themen