ALAssetsLibrary-代码操作iOS相册资源

     我们可以使用使用:UIImagePickerController类来完成相册和摄像头的调用。

     但是毕竟是人家封装好的,是否有方法读取相册中的所有图片呢?

     答案是肯定的:ALAssetsLibrary。


     上代码:

self.view.backgroundColor = [UIColor whiteColor];
    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];
                                                          if (image != nil){
                                                              self.imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
                                                              self.imageView.contentMode = UIViewContentModeScaleAspectFit;
                                                              self.imageView.image = image;
                                                              [self.view addSubview:self.imageView];
                                                          } else {
                                                              NSLog(@"Failed to create the image.");
                                                          } });
                                                  }
                                              }];
                                          }
                                          failureBlock:^(NSError *error) {
                                              NSLog(@"Failed to enumerate the asset groups.");
                                          }];
        
    });

  代码说明:


   1.就是先遍历所有相册,然后,遍历每个相册中的第一张图片。


   2.ALAssetsGroup -- 一个相册。


  3.ALAsset是指每一个ALAsset代表一个单一资源文件(也就是一张图片,或者一个视频文件)


  4.ALAssetRepresentation:ALAssetRepresentation封装了ALAsset,包含了一个资源文件中的很多属性。(可以说是ALAsset的不同的表示方式,本质上都表示同一个资源文件)


你可能感兴趣的:(ios,相册)