【问题标题】:How to use ALAssetLibrary to get Local Photos in Camera Roll如何使用 ALAssetLibrary 获取相机胶卷中的本地照片
【发布时间】:2012-06-19 03:15:16
【问题描述】:
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 刷新。看起来不舒服。 非常感谢!

我应该写什么?

【问题讨论】:

    标签: ios camera photo grand-central-dispatch alassetslibrary


    【解决方案1】:

    试试这个

    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];
    
                });
            } 
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2011-11-26
        • 1970-01-01
        • 1970-01-01
        • 2014-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-21
        • 1970-01-01
        相关资源
        最近更新 更多