2
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
                           usingBlock:libraryGroupsEnumeration 
                         failureBlock:failureblock];
ALAssetsGroupEnumerationResultsBlock groupEnumerAtion = ^(ALAsset *result, NSUInteger index, BOOL *stop){
        if (result!=NULL) {

            if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {

                [self._dataArray addObject:result];
            }

        }
    };

ALAssetsLibraryGroupsEnumerationResultsBlock
    libraryGroupsEnumeration = ^(ALAssetsGroup* group, BOOL* stop){
        //within the group enumeration block.filter to enumerate just photos.
        [group setAssetsFilter:[ALAssetsFilter allPhotos]];
        if (group!=nil) {
            NSString *g=[NSString stringWithFormat:@"%@",group];
            NSLog(@"gg:%@",g);//gg:ALAssetsGroup - Name:Camera Roll, Type:Saved Photos, Assets count:71
            [group enumerateAssetsUsingBlock:groupEnumerAtion];
        }
        else {
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                [self saveToDB:self._dataArray];

            });
        }

    };

假设我的相机胶卷有 100 张照片,我想将前 30 张照片保存到我的数据库中。但是在上面的代码中,我必须等待 100 条结果钓鱼。30 条写入数据库后,继续获取另外 30 条直到结束。因为获取 100 甚至更多的照片会延迟我的 UI 刷新。看起来不舒服。非常感谢!

我应该写什么。?

4

2 回答 2

0

您应该尝试在后台线程上保存到数据库,并让 CoreData(假设您使用 CoreData)通过处理 NSManagedObjectContextDidSaveNotification 在主线程上更新 NSManagedObjectContext。

您仍然可以在 30 张照片后保存,这可能会减轻一些 I/O 压力,但您必须测试性能。

于 2012-07-24T09:19:40.553 回答
0

试试这个

if (result!=NULL) {

    if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto])         {

          [self._dataArray addObject:result];

            if([self._dataArray count] == 30){

               dispatch_async(dispatch_get_global_queue(0, 0), ^{

                 NSArray *array = [[NSArray alloc] initWithArray:self._dataArray]; //please change the array declaration to top of this method. Because the Block will not allow to do it here.               
                 [self._dataArray removeAllObjects];
                 [self saveToDB:array];
                 //array release,if not using ARC

           });
       }
    }

}

if (group!=nil) {

            [group enumerateAssetsUsingBlock:groupEnumerAtion];
        }
        else if([self._dataArray count] > 0) {

              dispatch_async(dispatch_get_global_queue(0, 0), ^{

                    [self saveToDB:self._dataArray];

            });
        } 
于 2013-02-11T01:59:01.947 回答