简体   繁体   English

如何获取存储在设备中的所有图像并将其显示为iPhone SDK中的图库

[英]How to get all the images stored in the device and show them as gallery in iPhone sdk

I want to get all the images stored in the device and show them as gallery in my iPhone app. 我想获取存储在设备中的所有图像,并在我的iPhone应用程序中将它们显示为图库。 UIImagePickerController shows all the photos in thumbnail view and on selecting each photo we get the data of that particular selected image only, instead i want to get all the photos at one shot and show them as gallery. UIImagePickerController以缩略图视图显示所有照片,并且在选择每张照片时,我们仅获得该特定所选图像的数据,相反,我想一次拍摄所有照片并将它们显示为图库。 Is this possible, if yes please tell me how to do this. 这可能吗,如果可以,请告诉我该怎么做。 What is the use of AssetLibrary ?. AssetLibrary的用途是什么? Does it help what I need to achieve.If yes, please tell me how to achieve this using AssetLibrary. 它对我需要实现的目标有帮助吗?如果是,请告诉我如何使用AssetLibrary实现此目标。

An instance of ALAssetsLibrary provides access to the videos and photos that are under the control of the Photos application. ALAssetsLibrary实例提供对在“照片”应用程序控制下的视频和照片的访问。

The library includes those that are in the Saved Photos album, those coming from iTunes, and those that were directly imported into the device. 该库包括“已保存的照片”相册中的文件,来自iTunes的文件以及直接导入设备的文件。 You use it to retrieve the list of all asset groups and to save images and videos into the Saved Photos album. 您可以使用它来检索所有资产组的列表,并将图像和视频保存到“保存的照片”相册中。

ELCImagePickerController is an Example project for getting all the photos in the iPhone Photo Album. ELCImagePickerController是一个示例项目,用于获取iPhone相册中的所有照片。

You can get all photos from device using Asset Library. 您可以使用素材资源库从设备中获取所有照片。 I implemented this in my app 我在我的应用程序中实现了

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


}

call this method in ViewDidLoad or where you need. 在ViewDidLoad或您需要的地方调用此方法。 I hope this will help you. 我希望这能帮到您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM