ALAssetsGroup详解

 

ALAssetsGroup类是系统用于映射相册资源中的每个相册,可以通过该类获取相册中的资源文件,并且能向相册中添加资源文件

   

    //保存图片到系统默认的相册中,使用cgimageref的形式,并且选择图片以什么旋转方向的形式保存,并返回照片的url地址

    /*
     typedef enum {
     ALAssetOrientationUp,            // default orientation
     ALAssetOrientationDown,          // 180 deg rotation
     ALAssetOrientationLeft,          // 90 deg CCW
     ALAssetOrientationRight,         // 90 deg CW
     ALAssetOrientationUpMirrored,    // as above but image mirrored along other axis. horizontal flip
     ALAssetOrientationDownMirrored,  // horizontal flip
     ALAssetOrientationLeftMirrored,  // vertical flip
     ALAssetOrientationRightMirrored, // vertical flip
     } ALAssetOrientation;
     */
    UIImage* image = [UIImage imageNamed:@"test.png"];
    [_library writeImageToSavedPhotosAlbum:[image CGImage] orientation:ALAssetOrientationLeft completionBlock:^(NSURL *assetURL, NSError *error) {
        NSLog(@"save image:%@",assetURL);
        //通过ALAssetsLibrary迭代取出所有相册
        [_library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
            NSString* groupname = [group valueForProperty:ALAssetsGroupPropertyName];
            //如果相册的名称是test的时候,对其进行操作
            if ([groupname isEqualToString:@"test"]) {
                //设置相册组的筛选条件,ALAssetsFilter类表示筛选条件,allPhotos代表相册只包含相片,allVideos代表只包含视频,allAssets代表包含所有资源
                [group setAssetsFilter:[ALAssetsFilter allPhotos]];
                //通过刚保存的照片的url,把保存到默认相册的照片也保存到test相册中
                [_library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
                    //添加资源到指定的相册
                    [group addAsset:asset];
                    //获取相册中一共的资源数量
                    int count = [group numberOfAssets];
                    NSLog(@"count:%d",count);
                    dispatch_queue_t main = dispatch_get_main_queue();
                    dispatch_async(main, ^{
                        //获取相册的封面图片
                        CGImageRef poster = [group posterImage];
                        [imageView setImage:[UIImage imageWithCGImage:poster]];
                    });
                    //NSString *const ALAssetsGroupPropertyName;
                    //NSString *const ALAssetsGroupPropertyType;
                    //NSString *const ALAssetsGroupPropertyPersistentID;
                    //NSString *const ALAssetsGroupPropertyURL;
                    //查看相册的名字
                    NSLog(@"ALAssetsGroupPropertyName:%@",[group valueForProperty:ALAssetsGroupPropertyName]);
                    //查看相册的类型
                    NSLog(@"ALAssetsGroupPropertyType:%@",[group valueForProperty:ALAssetsGroupPropertyType]);
                    //查看相册的存储id
                    NSLog(@"ALAssetsGroupPropertyPersistentID:%@",[group valueForProperty:ALAssetsGroupPropertyPersistentID]);
                    //查看相册存储的位置地址
                    NSLog(@"ALAssetsGroupPropertyURL:%@",[group valueForProperty:ALAssetsGroupPropertyURL]);
                    //按遍历顺序获取指定索引的资源,遍历顺序可以是先序或倒序
                    /*
                     enum {
                     NSEnumerationConcurrent = (1UL << 0),
                     NSEnumerationReverse = (1UL << 1),
                     };
                     typedef NSUInteger NSEnumerationOptions;
                     */
                    [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:0] options:NSEnumerationConcurrent usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                        
                    }];
                    //按顺便遍历获取相册中所有的资源,index代表资源的索引,stop赋值为false时,会停止遍历
                    [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                        
                    }];
                     //按顺便遍历获取相册中所有的资源,遍历顺序可以是先序或倒序,index代表资源的索引,stop赋值为false时,会停止遍历
                    [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                        
                    }];
                    
                } failureBlock:^(NSError *error) {
                    
                }];
            }
        } failureBlock:^(NSError *error) {
            
        }];
    }];

转载于:https://www.cnblogs.com/allanliu/p/4213359.html

源码SGImagePickerController,1.需要导入这个头文件 #import 2.获取相册分组 - (NSMutableArray *)groups{ if (_groups == nil) { _groups = [NSMutableArray array]; dispatch_async(dispatch_get_main_queue(), ^{ [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) { if(group){ [_groups addObject:group]; [self.tableView reloadData]; } } failureBlock:^(NSError *error) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"访问相册失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alertView show]; }]; }); } return _groups; } 3.遍历一组中的资源,包括图片视频等,我们只需要图片 - (void)setGroup:(ALAssetsGroup *)group{ _group = group; [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) { if (asset == nil) return ; if (![[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {//不是图片 return; } SGAssetModel *model = [[SGAssetModel alloc] init]; model.thumbnail = [UIImage imageWithCGImage:asset.thumbnail]; model.imageURL = asset.defaultRepresentation.url; [self.assetModels addObject:model]; }]; } 4.遍历可以拿到图片缩略图,原图的URL 图片拍摄地点 拍摄时间等信息,我们只需要缩略图用来展示,原图URL用来获取原图 根据URL获取原图,系统应该是在子线程中的获取原图,注意此处!!! - (void)originalImage:(void (^)(UIImage *))returnImage{ ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init]; [lib assetForURL:self.imageURL resultBlock:^(ALAsset *asset) { ALAssetRepresentation *rep = asset.defaultRepresentation; CGImageRef imageRef = rep.fullResolutionImage; UIImage *image = [UIImage imageWithCGImage:imageRef scale:rep.scale orientation:(UIImageOrientation)rep.orientation]; if (image) { returnImage(image); } } failureBlock:^(NSError *erro
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值