码迷,mamicode.com
首页 > 其他好文 > 详细

ALAssets的两种用法

时间:2015-10-09 19:46:04      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

一:

ALAssetsGroupEnumerationResultsBlock resultsBlock = ^(ALAsset *result, NSUInteger index, BOOL *stop) {

        if (result && [[result valueForProperty:ALAssetPropertyType] isEqual:ALAssetTypePhoto]) {

            ZWPhoto *aPhoto = [[ZWPhoto alloc] initWithAsset:result];

            aPhoto.checked = false;

            aPhoto.originPhotoNeeded = false;

            

            [self.photosArray addObject:aPhoto];

        } else if (self.photosArray.count > 0) {

            [self.collectionView reloadData];

        }

    };

    

    [self.assetsGroup enumerateAssetsUsingBlock:resultsBlock];

 

二:

self.assetsLibrary = [[ALAssetsLibrary alloc] init];

    dispatch_queue_t dispatchQueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_async(dispatchQueue, ^(void) {

        // 遍历所有相册

        [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll

                                          usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

                                              // 遍历每个相册中的项ALAsset

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

                                                  

                                                  __block BOOL foundThePhoto = NO;

                                                  if (foundThePhoto){

                                                      *stop = YES;

                                                  }

                                                  // ALAsset的类型

                                                  NSString *assetType = [result valueForProperty:ALAssetPropertyType];

                                                  if ([assetType isEqualToString:ALAssetTypePhoto]){

                                                      foundThePhoto = YES;

                                                      *stop = YES;

                                                      ALAssetRepresentation *assetRepresentation =[result defaultRepresentation];

                                                      CGFloat imageScale = [assetRepresentation scale];

                                                      UIImageOrientation imageOrientation = (UIImageOrientation)[assetRepresentation orientation];

                                                      dispatch_async(dispatch_get_main_queue(), ^(void) {

                                                          CGImageRef imageReference = [assetRepresentation fullResolutionImage];

                                                          // 对找到的图片进行操作

                                                          UIImage *image =[[UIImage alloc] initWithCGImage:imageReference scale:imageScale orientation:imageOrientation];

                                                          _myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];

                                                          _myImageView.image = image;

                                                          [self.view addSubview:_myImageView];

                                                          if (image != nil){

                                                              //获取到第一张图片

                                                          } else {

                                                              NSLog(@"Failed to create the image.");

                                                          } });

                                                  }

                                              }];

                                          }

                                        failureBlock:^(NSError *error) {

                                            NSLog(@"Failed to enumerate the asset groups.");

                                        }];

        

    });

ALAssets的两种用法

标签:

原文地址:http://www.cnblogs.com/wskgjmhh/p/4864729.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!