获取照片库中所有照片


方法一:

 

- (void)loadAssets {

    // Runin the background as it takes a while to get all assets from the library

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

       

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

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

       

        //Process assets

        void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {

            if (result != nil) {

               [assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];

 

                NSString *assetType = [result valueForProperty:ALAssetPropertyType];

               

                if ([assetType isEqualToString:ALAssetTypePhoto]) {

                    // photo

                    NSURL *url = result.defaultRepresentation.url;

                    [_assetLibrary assetForURL:url

                                   resultBlock:^(ALAsset *asset) {

                                      if (asset) {

                                          @synchronized(_photoAssets) {

                                              [_photoAssets addObject:asset];

                                          }

                                       }

                                   }

                                  failureBlock:^(NSError *error){

                                     NSLog(@"operation was notsuccessfull!");

                                 }];

                }

                else if ([assetType isEqualToString:ALAssetTypeVideo]) {

                    // video

                    NSURL *url = result.defaultRepresentation.url;

                    [_assetLibrary assetForURL:url

                                   resultBlock:^(ALAsset *asset) {

                                      if (asset) {

                                          @synchronized(_videoAssets) {

                                               [_videoAssets addObject:asset];

                                          }

                                      }

                                   }

                                  failureBlock:^(NSError *error){

                                      NSLog(@"operation was not successfull!");

                                 }];

                }

                else if ([assetType isEqualToString:ALAssetTypeUnknown]) {

                    // unknown

                }

            }

        };

       

        //Process groups

        void (^ assetGroupEnumerator) (ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) {

            if (group != nil) {

                [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:assetEnumerator];

                [assetGroups addObject:group];

            }

            else {

                // 上传完

                [self getPhotoAsset];

//                [self getVideoAsset];

           }

       };

       

       // Process!

       [self.assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll

                                         usingBlock:assetGroupEnumerator

                                       failureBlock:^(NSError*error) {

                                           NSLog(@"There is an error");

                                       }];

    });

}

 

 

 

方法二:

 

- (void)cameraRollButtonPressed

{

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

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

    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

        if (group) {

            [group setAssetsFilter:[ALAssetsFilter allPhotos]];

            [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {

                if (result) {

                    Photo *photo = [[Photo alloc] init];

                    photo.thumbnail = [UIImage imageWithCGImage:result.thumbnail];

                    photo.date = [result valueForProperty:ALAssetPropertyDate];

                    [datasource addObject:photo];

                    NSLog(@"%i", [datasource count]);

                }

            }];

        } else {

            [self performSelectorOnMainThread:@selector(showPhotoCollectionController:) withObject:datasource waitUntilDone:NO];           

        }

    } failureBlock:^(NSError *error) {

        NSLog(@"Failed.");

    }];

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值