【问题标题】:How to get all the images stored in the device and show them as gallery in iPhone sdk如何获取存储在设备中的所有图像并将它们显示为 iPhone sdk 中的图库
【发布时间】:2012-06-26 03:28:48
【问题描述】:

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

【问题讨论】:

  • 查看这篇苹果开发者文章:developer.apple.com/library/ios/#DOCUMENTATION/AudioVideo/…>

标签: iphone objective-c ios


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

    -(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 或您需要的地方调用此方法。我希望这会对你有所帮助。

    【讨论】:

    • 缺少收集的所有照片,计数
    猜你喜欢
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    • 2017-03-04
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-10
    相关资源
    最近更新 更多