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

                                            }];

            

        });

  • 相关阅读:
    redis报错io.lettuce.core.RedisCommandTimeoutException: Command timed out after
    showloading showtoast一起用
    uniapp,mpvue微信,支付宝兼容性说明
    uniapp 自定义组件
    Vue 函数式组件 functional
    mac uninstall node ,npm from official
    mpvue问题记录:组件方法执行机制
    linux 文件 分割重组
    MySql日期操作
    .NET Core连接数据库
  • 原文地址:https://www.cnblogs.com/wskgjmhh/p/4864729.html
Copyright © 2020-2023  润新知