【问题标题】:To Get all Images from Photo Library in iphone从 iphone 的照片库中获取所有图像
【发布时间】:2012-07-07 06:20:24
【问题描述】:

我尝试使用 AssetsLibrary 从照片库中检索图像并显示在我的应用中。 我得到了照片的 URL 路径,但我不知道如何通过它获取照片。

我的代码如下:

   NSMutableArray* assetURLDictionaries = [[NSMutableArray 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(@"result is:%@",result);
                NSLog(@"asset URLDictionary is:%@",assetURLDictionaries);
                NSURL *url= (NSURL*) [[result defaultRepresentation]url]; 

                [library assetForURL:url
                         resultBlock:^(ALAsset *asset) { [assetsp addObject:asset]; }
                        failureBlock:^(NSError *error){ NSLog(@"test:Fail"); } ]; 
           } 
       }
   };


   NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
   void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop){
        NSLog(@"hi");
        if(group != nil) {
            [group enumerateAssetsUsingBlock:assetEnumerator];
            [assetGroups addObject:group];

            NSLog(@"Number of assets in group :%d",[group numberOfAssets]);           
        }
   };

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

   [library enumerateGroupsWithTypes:ALAssetsGroupAll
                          usingBlock:assetGroupEnumerator
                        failureBlock:^(NSError *error) {NSLog(@"A problem occurred");}];

【问题讨论】:

  • 好的,告诉我assetsp BTW 是什么?
  • 好的,你有解决方案吗?
  • assetsp 是我的 NSMutableArray 存储所有图像的地方
  • 所以还是没有运气?需要帮忙?我为这个问题找到了一些新的解决方法,请参阅我的新答案
  • 查看我的新答案 1,它为您提供了新的方法

标签: iphone objective-c ios xcode4 alassetslibrary


【解决方案1】:

此代码取自 here 的一些 ELCImagePickerController 示例 为了简化,做了一些修改。

希望对你有帮助

[self.assetGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) 
     {         
         if(result == nil) 
             return;
         UIImageView *assetImageView = [[UIImageView alloc] initWithFrame:viewFrames];
        [assetImageView setContentMode:UIViewContentModeScaleToFill];
        [assetImageView setImage:[UIImage imageWithCGImage:[result thumbnail]]];
     }];    

快乐编码:)

新答案

随便用

[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]

直接存储图像,而不是在将资产存储在assetsp时存储资产

如下

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(@"result is:%@",result);
                NSLog(@"asset URLDictionary is:%@",assetURLDictionaries);
                NSURL *url= (NSURL*) [[result defaultRepresentation]url]; 

                [library assetForURL:url
                         resultBlock:^(ALAsset *asset) { //Your line
                                                           //[assetsp addObject:asset];
                                                         //My Changed line will store image directly to assetsp
                                                           [assetsp addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];
                                                       }
                        failureBlock:^(NSError *error){ NSLog(@"test:Fail"); } ]; 
           } 
       }
   };

新答案 1

尝试使用此代码将 ALAsset 数据存储在可变数组中

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

    for(ALAsset *asset in _assets) {

        NSMutableDictionary *workingDictionary = [[NSMutableDictionary alloc] init];
        [workingDictionary setObject:[asset valueForProperty:ALAssetPropertyType] forKey:@"UIImagePickerControllerMediaType"];
        [workingDictionary setObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]] forKey:@"UIImagePickerControllerOriginalImage"];
        [workingDictionary setObject:[[asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]] forKey:@"UIImagePickerControllerReferenceURL"];

        [returnArray addObject:workingDictionary];

要在 UIImageView 中获取图像,只需这样做

NSDictionary *dict = [info objectAtIndex:i];
        UIImageView *imageview = [[UIImageView alloc] initWithImage:[dict objectForKey:UIImagePickerControllerOriginalImage]];
        [imageview setContentMode:UIViewContentModeScaleAspectFit];

【讨论】:

  • 我得到了所有的图像,但它没有显示
  • 什么意思得到了所有图片但没有显示?
  • 是的,我初始化了它,我得到了 NSLOG 中的所有图像,但它们没有显示在视图中
  • NSLOG 答案:“”、“”、“”、“”、“” , “”, “”, “”, “”, “”, “”, “ ", "", "" ) 2012-07-07 19:17:07.963 Carousel[5901:207] 图像是:>
【解决方案2】:

我的代码如下:

- (void)viewDidLoad
{
    carousel.type = iCarouselTypeCoverFlow2;
    [super viewDidLoad];
    xy =[[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(@"result is:%@",result);
                NSLog(@"asset URLDictionary is:%@",assetURLDictionaries);
                NSURL *url= (NSURL*) [[result defaultRepresentation]url]; 

                [library assetForURL:url
                         resultBlock:^(ALAsset *asset) {
                             [xy addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];
                             NSLog(@" xy is:%@",xy);
                             image =[ [UIImageView alloc ] initWithImage:[xy objectAtIndex:0]];
                             NSLog(@"image is:%@",image);
                         }
                         failureBlock:^(NSError *error){ NSLog(@"test:Fail"); } ]; 
            } 
        }
    };

    NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
    void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
        NSLog(@"hello");
        if(group != nil) {
            [group enumerateAssetsUsingBlock:assetEnumerator];
            [assetGroups addObject:group];
            NSLog(@"Number of assets in group :%d",[group numberOfAssets]);
            NSLog(@"asset group is:%@",assetGroups);
        }
    };

    assetGroups = [[NSMutableArray alloc] init];

    [library enumerateGroupsWithTypes:ALAssetsGroupAll
             usingBlock:assetGroupEnumerator
             failureBlock:^(NSError *error) {NSLog(@"A problem occurred");}];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多