zoukankan      html  css  js  c++  java
  • ALAssets的两种用法

    一:

    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.");

                                            }];

            

        });

  • 相关阅读:
    Android自定义控件之仿美团下拉刷新
    Android性能优化之Bitmap的内存优化
    基于openfire+smack即时通讯instant message开发
    Android各组件/控件间通信利器之EventBus
    android的task任务栈
    Activity的启动模式
    Android 自定义View (一)
    Android之Handler用法总结
    Android中轻松使用线程
    Android 中Activity,Window和View之间的关系
  • 原文地址:https://www.cnblogs.com/wskgjmhh/p/4864729.html
Copyright © 2011-2022 走看看