查看当前组是否已使用ALAssetsLibraryChangedNotification删除的正确方法是什么?

时间:2014-05-16 23:34:41

标签: xcode notifications alassetslibrary

删除某个群组后,–groupForURL:resultBlock:failureBlock:将无法再找到该群组。现在您可能会说您必须将通知中收到的NSURL与在当前组中使用ALAssetsGroupPropertyURL时获得的NSURL进行比较。但是,当当前组有一个过滤器时,NSURL将附加&filter=并且NSURL将不匹配。

因此,如果您有一个ALAssetsGroup类型的当前群组,并且您收到ALAssetsLibraryChangedNotification的通知。如何检查当前组是否被移除?

到目前为止我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.collectionView.alwaysBounceVertical    = YES;
    self.collectionView.backgroundColor         = [UIColor whiteColor];
    self.collectionView.opaque                  = YES;
    self.collectionView.contentInset            = UIEdgeInsetsMake(9, 0, 0, 0);

    [self.collectionView registerClass:[CMPhotoCollectionViewCell class] forCellWithReuseIdentifier:@"PhotoCell"];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(assetsLibraryDidChange:) name:ALAssetsLibraryChangedNotification object:nil];

    self.title = [self.album.group valueForProperty:ALAssetsGroupPropertyName];

    if (!self.assets)
        _assets = [NSMutableArray new];
    else
        [self.assets removeAllObjects];

    ALAssetsGroupEnumerationResultsBlock assetsEnumerationBlock = ^(ALAsset *result, NSUInteger index, BOOL *stop)
    {
        if (result) [self.assets addObject:result];
    };

    [self.album.group enumerateAssetsUsingBlock:assetsEnumerationBlock];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.collectionView reloadData];
}

#pragma mark -
#pragma mark Assets Library

- (void)assetsLibraryDidChange:(NSNotification *)notification
{
    if (notification.userInfo[ALAssetLibraryDeletedAssetGroupsKey])
    {
        NSLog (@"Album removed: %@", notification.userInfo[ALAssetLibraryDeletedAssetGroupsKey]);
        NSLog (@"This album: %@", self.album.group);
        NSLog (@"%@", [self.album.group valueForProperty:ALAssetsGroupPropertyURL]);

        // I get a NSSet with NSURLs of groups that have been removed.
        // However the NSURL seems to be always without filter, if my current item
        // has a filter on it, the URLS don't match.
    }
}

#pragma mark - UICollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
{
    return self.assets.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CMPhotoCollectionViewCell    *cell      = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoCell" forIndexPath:indexPath];
    ALAsset                     *asset      = self.assets[indexPath.row];
    UIImage                     *thumbnail  = [UIImage imageWithCGImage:asset.thumbnail];

    cell.imageView.image = thumbnail;

    return cell;
}

0 个答案:

没有答案