0

我的应用程序的一部分有一个类似于 Apple 的照片应用程序(网格状视图)的照片浏览器。为了在原始照片应用程序发生任何变化时刷新我的照片,我注册了 ALAssetsLibraryChangedNotification

self.assetsLibrary = [[ALAssetsLibrary alloc] init];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveLibraryChangedNotification:) name:ALAssetsLibraryChangedNotification object:self.assetsLibrary];

在“receiveLibraryChangedNotification”方法中 - 我检查 userInfo 是否有 ALAssetLibraryUpdatedAssetsKey 然后调用刷新照片方法。

 - (void) receiveLibraryChangedNotification:(NSNotification *) notif{
        NSDictionary *userInfo = [notif userInfo];
        if(userInfo){
            id updatedAssets = [userInfo objectForKey:ALAssetLibraryUpdatedAssetsKey];

            if(updatedAssets){

                   [self refreshPhotos];
                }
    }




- (void) refreshPhotos {

    self.assetArray = nil;

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^(void){
                [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

                    if(group){
                        [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [group numberOfAssets])] options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *shouldStop) {
                            if(result){

                                if([[result valueForProperty:@"ALAssetPropertyType"] isEqualToString:@"ALAssetTypePhoto"]){
                                    [self.assetArray addObject:result];
                                }
                            }
                        }];
                    }
                } failureBlock:^(NSError *error) {

                    DebugLog(@"error >>> %@",[error description]);

                }];
            });
}

我面临的问题是有时通知被多次触发并且应用程序崩溃

[self.assetArray addObject:result];

有错误 -

malloc: *** error for object 0x4aa9000: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

或者

malloc: *** error for object 0x16fd3e74: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug

有时我也没有在通知的 userInfo 中收到 ALAssetLibraryUpdatedAssetsKey,所以照片永远不会刷新。

有人可以指导我正确的方向。

提前致谢。

4

1 回答 1

0

self.assetArray在赋值后分配nil或写入[self.assetArray removeAllObjects]而不是分配给nil.

- (void) refreshPhotos {

    self.assetArray = nil;
    self.assetArray = [[NSMutableArray alloc] init];

    //or remove the top 2 lines and try `[self.assetArray removeAllObjects]`

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^(void){
            [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

                if(group){
                    [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [group numberOfAssets])] options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *shouldStop) {
                        if(result){

                            if([[result valueForProperty:@"ALAssetPropertyType"] isEqualToString:@"ALAssetTypePhoto"]){
                                [self.assetArray addObject:result];
                            }
                        }
                    }];
                }
            } failureBlock:^(NSError *error) {

                DebugLog(@"error >>> %@",[error description]);

            }];
        });
}

有时我也没有在通知的 userInfo 中收到 ALAssetLibraryUpdatedAssetsKey,

有四个键用于从ALAssetsLibraryChangedNotification通知的用户信息字典中获取值。

NSString * const ALAssetLibraryUpdatedAssetsKey;
NSString * const ALAssetLibraryInsertedAssetGroupsKey;
NSString * const ALAssetLibraryUpdatedAssetGroupsKey;
NSString * const ALAssetLibraryDeletedAssetGroupsKey;
于 2014-01-09T03:51:43.413 回答