ALAssetsLibrary Notification和enumerateObjectsUsingBlock

时间:2014-04-30 01:56:44

标签: ios objective-c grand-central-dispatch alassetslibrary nsnotification

我正在构建一个iOS应用程序,允许用户拍照并将其保存到自定义相册,该相册会在应用中使用相册中的照片填充UICollectionView。我在网上找到了很多关于如何做到这一点的例子,但我无法通过专辑中没有出现的最新图片来解决顽固的问题。我结束了使用通知和串行调度队列的工作,但我认为它可以改进。

以下是我如何保存图片然后重新填充UICollectionView:

当我需要枚举相册时,我构建了一个方法来调用(viewDidLoad,一旦我收到了ALAssetsLibraryChangedNotification)。

-(void)setupCollectionView {
    _assets = [@[] mutableCopy];
    __block NSMutableArray *tmpAssets = [@[] mutableCopy];

    // This grabs a static instance of the ALAssetsLibrary
    ALAssetsLibrary *assetsLibrary = [ProfileViewController defaultAssetsLibrary];    

    // Enumerate through all of the ALAssets (photos) in the user’s Asset Groups (Folders)
    [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:albumName]) {
            // Make sure we have the group... (this may be redundant) 
            if (group == nil){
                return;
            }

            // I read in some SO posts that setting the filter might help, but I don't think it does
            NSLog(@"Refresh pictures - %d",[group numberOfAssets]);
            [group setAssetsFilter:[ALAssetsFilter allPhotos]];//this will cause group to reload
            if (group!=nil) {
                [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                    if(result){
                        [tmpAssets addObject:result];
                    }else{
                        *stop = YES;
                    }
                }];
            }

            self.assets = tmpAssets;

            // Reload the UICollectionView
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectionView reloadData];
                [self.collectionView.collectionViewLayout invalidateLayout];
            });
        }
    } failureBlock:^(NSError *error) {
        NSLog(@"Error loading images %@", error);
    }];
}

最初这似乎工作得很好,但有时候,在将图像写入自定义相册后调用此图片时,刚刚拍摄的照片不会出现在集合视图中。照片将保存到图书馆(它显示在照片应用程序中),退出并重新运行应用程序后,照片会出现,但枚举不包括最新照片。

正如关于此事的一些Stack Overflow帖子的建议,我尝试实施ALAssets'通过侦听ALAssetsLibraryChangedNotification来通知,但我遇到了一些问题。这是我收到通知后我正在做的事情:

- (void) handleAssetChangedNotifiation:(NSNotification *)notification {
    NSDictionary *userInfo = notification.userInfo;
    NSSet *updateAssets = userInfo[ALAssetLibraryUpdatedAssetsKey];
    if (updateAssets.count>0) {        
        dispatch_sync(photoLoadQueue, ^{
            [self setupCollectionView];
        });
    }
}

这对我来说似乎很好。我正在检查ALAssetLibraryUpdatedAssetsKey中是否有任何数据,然后将其丢弃在调度队列上,我发现我开始使用该调度队列时,我收到了几个用于保存单张照片的通知。这是我在将照片写入自定义库后收到的通知:

Received notification: NSConcreteNotification 0x15ed4af0 {name = ALAssetsLibraryChangedNotification; object = <ALAssetsLibrary: 0x15db9e80>; userInfo = {
ALAssetLibraryUpdatedAssetGroupsKey = "{(\n    assets-library://group/?id=43E80EF8-EA91-4C71-AFD2-EBDCB53A1D53\n)}";
ALAssetLibraryUpdatedAssetsKey = "{(\n    assets-library://asset/asset.JPG?id=63E43AF3-3729-43E9-806C-BE463116FDA2&ext=JPG,\n    assets-library://asset/asset.JPG?id=FA2ED24E-DF0F-49A3-85B8-9C181E4077A4&ext=JPG,\n    assets-library://asset/asset.JPG?id=A0B66E2E-6EA1-462C-AD93-DB3087BA65D8&ext=JPG\n)}";}}

Received notification: NSConcreteNotification 0x15d538c0 {name = ALAssetsLibraryChangedNotification; object = <ALAssetsLibrary: 0x15db9e80>; userInfo = {
ALAssetLibraryUpdatedAssetsKey = "{(\n    assets-library://asset/asset.JPG?id=FBDD2086-EC76-473F-A23E-4F4200C0A6DF&ext=JPG\n)}";}}

Received notification: NSConcreteNotification 0x15dc9230 {name = ALAssetsLibraryChangedNotification; object = <ALAssetsLibrary: 0x15db9e80>; userInfo = {}}

Received notification: NSConcreteNotification 0x15df5b40 {name = ALAssetsLibraryChangedNotification; object = <ALAssetsLibrary: 0x15db9e80>; userInfo = {}}

Received notification: NSConcreteNotification 0x15d6a050 {name = ALAssetsLibraryChangedNotification; object = <ALAssetsLibrary: 0x15db9e80>; userInfo = {}}

Received notification: NSConcreteNotification 0x15d379e0 {name = ALAssetsLibraryChangedNotification; object = <ALAssetsLibrary: 0x15db9e80>; userInfo = {
ALAssetLibraryUpdatedAssetGroupsKey = "{(\n    assets-library://group/?id=1E76AFA7-89B4-4277-A175-D7C8E62E49D0&filter=1,\n    assets-library://group/?id=1E76AFA7-89B4-4277-A175-D7C8E62E49D0\n)}";
ALAssetLibraryUpdatedAssetsKey = "{(\n    assets-library://asset/asset.JPG?id=FBDD2086-EC76-473F-A23E-4F4200C0A6DF&ext=JPG\n)}";

Received notification: NSConcreteNotification 0x15df7bf0 {name = ALAssetsLibraryChangedNotification; object = <ALAssetsLibrary: 0x15db9e80>; userInfo = {
ALAssetLibraryUpdatedAssetGroupsKey = "{(\n    assets-library://group/?id=8E390051-E107-42BC-AE55-24BA35966642\n)}";}}

Received notification: NSConcreteNotification 0x15d40360 {name = ALAssetsLibraryChangedNotification; object = <ALAssetsLibrary: 0x15db9e80>; userInfo = {
ALAssetLibraryUpdatedAssetsKey = "{(\n    assets-library://asset/asset.JPG?id=5A45779A-C3C1-4545-9BD6-88F094FFA5B9&ext=JPG\n)}";}}

为什么我收到这么多通知?我没有看到一种方法来查找一条特定的消息,一旦图像准备好就会告诉我,所以我使用该调度队列来连续地触发枚举。有没有人对此或解决方案有任何经验?我的工作,但我肯定比我应该做更多的工作。

只是为了完成,这是我保存图像的方法(我可能不应该像我一样嵌套这些块,但我这样做是为了尝试修复添加的图像未被发现):

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *originalImage, *editedImage, *imageToSave;
    editedImage = (UIImage *) [info objectForKey:UIImagePickerControllerEditedImage];
    originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];
    if (editedImage) {
        imageToSave = editedImage;
    }else {
        imageToSave = originalImage;
    }

    // Get group/asset library/album
    __block ALAssetsGroup* groupToAddTo;
    ALAssetsLibrary *assetsLibrary = [ProfileViewController defaultAssetsLibrary];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:ALAssetsLibraryChangedNotification object:nil];

    [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:albumName]) {
        NSLog(@"found album %@", albumName);
        groupToAddTo = group;

        // save to album
        CGImageRef img = [imageToSave CGImage];

        // There's some orientation stuff here I pulled out for brevity's sake 

        [assetsLibrary writeImageToSavedPhotosAlbum:img orientation: alOrientation completionBlock:^(NSURL* assetURL, NSError* error) {
            if (error.code == 0) {
                [assetsLibrary assetForURL:assetURL resultBlock:^(ALAsset *asset) {
                // assign the photo to the album
                    if ([groupToAddTo valueForProperty:ALAssetsGroupPropertyURL] == nil){
                        NSLog(@"group properties are nil!");
                    }else {
                        if([groupToAddTo addAsset:asset]){
                            // If the asset writes to the library, listen for the notification
                            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAssetChangedNotifiation:) name:ALAssetsLibraryChangedNotification object:nil];
                        }else {
                            NSLog(@"Image Not Added!");
                        }
                    }
                }
                failureBlock:^(NSError* error) {
                    NSLog(@"failed to retrieve image asset:\nError: %@ ", [error localizedDescription]);
                }];
            }else {
                NSLog(@"saved image failed.\nerror code %i\n%@", error.code, [error localizedDescription]);
            }
        }];
    }}
    failureBlock:^(NSError* error) {
        NSLog(@"failed to enumerate albums:\nError: %@", [error localizedDescription]);}];

    [picker dismissViewControllerAnimated:YES completion:NULL];
}

1 个答案:

答案 0 :(得分:1)

要意识到的是,与ALAssetsLibrary相关联的块是异步。因此,像这样的代码不起作用:

        if (group!=nil) {
            [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                if(result){
                    [tmpAssets addObject:result];
                }else{
                    *stop = YES;
                }
            }];
        }
        self.assets = tmpAssets;

&#34; last&#34;行self.assets = tmpAssets,在块之前运行 - 因此tmpAssets永远无法正确设置。

对于与ALAssetsLibrary调用关联的所有块,情况也是如此。

一旦掌握了这一点,您就可以重新构建代码,以便一切按照正确的顺序进行。事实上,事实证明这是通过枚举块的额外传递的用途。正如我在书中写的那样:

  

我最初对这个奇怪的块枚举行为感到困惑,但有一天它的原因在于它闪现出来:这些块都被称为异步(在主线程上),意思是你的其他代码已经完成了运行,所以你有一个额外的通过块作为你第一次某事的机会,你可能会收集到的所有数据。以前的通行证。

所以你看,你希望你的代码结构更像这样:

        if (group!=nil) {
            [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                if(result){
                    [tmpAssets addObject:result];
                }else{
                    self.assets = tmpAssets; // <--- !!!!
                    // and now proceed to whatever the _next_ step is...!
                }
            }];
        }

所以,你看,你完全走错了路。摆脱所有您的通知,并根据这些新知识完全重构您的所有代码。一旦你理解了这个关于它是如何工作的这个简单但很难记录的事实,ALAssetsLibrary是完全一致的。