1

我想获取存储在设备中的所有图像并将它们显示为我的 iPhone 应用程序中的图库。UIImagePickerController 在缩略图视图中显示所有照片,在选择每张照片时,我们只获取该特定选定图像的数据,而不是我想一次获取所有照片并将它们显示为图库。这可能吗,如果可以,请告诉我该怎么做。AssetLibrary 有什么用?它对我需要实现的目标有帮助吗?如果是,请告诉我如何使用 AssetLibrary 实现这一目标。

4

2 回答 2

1

ALAssetsLibrary的一个实例提供对照片应用程序控制下的视频和照片的访问。

资料库包括“已保存照片”相册中的照片、来自 iTunes 的照片以及直接导入设备的照片。您可以使用它来检索所有资产组的列表,并将图像和视频保存到“已保存的照片”相册中。

ELCImagePickerController是一个示例项目,用于获取 iPhone 相册中的所有照片。

于 2012-06-26T04:08:45.160 回答
0

您可以使用资产库从设备中获取所有照片。我在我的应用程序中实现了这个

-(void)getAllPhotos{
   NSArray  *imageArray=[[NSArray alloc] init];
   NSMutableArray *mutableArray =[[NSMutableArray alloc]init];
    NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];

   ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

    void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
        if(result != nil) {
            if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
                [assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];
               // NSLog(@"assestlibrarymurari%@",assetURLDictionaries);

                NSURL *url= (NSURL*) [[result defaultRepresentation]url];

                [library assetForURL:url
                         resultBlock:^(ALAsset *asset) {
                             UIImage *img = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]];
                             if(img){
                                 [mutableArray addObject:img];
                             }
//                                 [mutableArray addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];

                             if ([mutableArray count]==count)
                             {
                                 imageArray=[[NSArray alloc] initWithArray:mutableArray];
                                 [self allPhotosCollected:imageArray];
                             }
                         }
                        failureBlock:^(NSError *error){ NSLog(@"operation was not successfull!"); } ];

            }
        }
    };

    NSMutableArray *assetGroups = [[NSMutableArray alloc] init];

    void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
        if(group != nil) {
            [group enumerateAssetsUsingBlock:assetEnumerator];
            [assetGroups addObject:group];
            NSLog(@"AssetGroup%@",assetGroups);
            count=[group numberOfAssets];
        }
    };

    assetGroups = [[NSMutableArray alloc] init];

    [library enumerateGroupsWithTypes:ALAssetsGroupAll
                           usingBlock:assetGroupEnumerator
                         failureBlock:^(NSError *error) {NSLog(@"There is an error");}];


}

在 ViewDidLoad 或您需要的地方调用此方法。我希望这能帮到您。

于 2015-06-24T06:22:24.083 回答