如何在没有UImagePickerController的情况下以编程方式在iphone中访问已保存图像的图像? [英] How to access Images from Saved Images programaically in iphone WITHOUT UImagePickerController?

查看:97
本文介绍了如何在没有UImagePickerController的情况下以编程方式在iphone中访问已保存图像的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何让用户从UIImagePickerController中选择一个图像,但我不希望这样。
我只是希望将NSArray图像存储在手机中,但我不想让用户参与(选择一个然后拥有该图像......),而是我创建了自己的自定义图像选择器控制器,并希望将源作为gallary。

I know how to let user select an image from UIImagePickerController, but I don't want that. I just want to have NSArray of images stored in the phone, but I don't want to involve user (to select a one and then have that image...),rather, I have created my own custom Image selector controller and want to have source as the gallary.

推荐答案

您可以使用AVFoundation和AssetsLibrary框架轻松完成此操作。以下是访问所有照片的代码:

You can easily do that using the AVFoundation and AssetsLibrary framework. Here is the code to access all the photos:

-(void)addPhoto:(ALAssetRepresentation *)asset
{
    //NSLog(@"Adding photo!");
    [photos addObject:asset];
}

-(void)loadPhotos
{
    photos = [[NSMutableArray alloc] init];    
    library = [[ALAssetsLibrary alloc] init];    

    // Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
    if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
    {
        [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) 
        {        
             // Within the group enumeration block, filter if necessary
             [group setAssetsFilter:[ALAssetsFilter allPhotos]];           
             [group enumerateAssetsUsingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop)
              {                                 
                  // The end of the enumeration is signaled by asset == nil.            
                  if (alAsset)
                  {
                      ALAssetRepresentation *representation = [alAsset defaultRepresentation];                      
                      [self addPhoto:representation];                      
                  }       
                  else
                  {
                      NSLog(@"Done! Count = %d", photos.count);
                      //Do something awesome
                  }
              }];
         }
         failureBlock: ^(NSError *error) {
         // Typically you should handle an error more gracefully than this.
         NSLog(@"No groups");                                 
         }];
    }
}

这篇关于如何在没有UImagePickerController的情况下以编程方式在iphone中访问已保存图像的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆