【问题标题】:app crashes when retrieving the ALAssets from ALAssetsGroupSavedPhotos group up on receiving ALAssetsLibraryChangedNotification在接收 ALAssetsLibraryChangedNotification 时从 ALAssetsGroupSavedPhotos 组中检索 ALAsset 时应用程序崩溃
【发布时间】:2014-01-08 11:07:01
【问题描述】:

我的应用程序的一部分有一个类似于 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,因此照片永远不会刷新。

谁能指引我正确的方向。

提前致谢。

【问题讨论】:

    标签: alassetslibrary photolibrary alassetsgroup


    【解决方案1】:

    分配 self.assetArraynil 分配之后或写[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]);
    
                }];
            });
    }
    

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

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

    NSString * const ALAssetLibraryUpdatedAssetsKey;
    NSString * const ALAssetLibraryInsertedAssetGroupsKey;
    NSString * const ALAssetLibraryUpdatedAssetGroupsKey;
    NSString * const ALAssetLibraryDeletedAssetGroupsKey;
    

    【讨论】:

      猜你喜欢
      • 2021-12-13
      • 2019-03-11
      • 2016-02-11
      • 1970-01-01
      • 1970-01-01
      • 2020-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多