ALAssetsLibrary类是代表系统中整个资源库,使用它可以访问资源库中的资源和保存照片,视频等功能。

_library = [[ALAssetsLibrary alloc]init];
    //判断当前应用是否能访问相册资源
    /*
     typedef NS_ENUM(NSInteger, ALAuthorizationStatus) {
     ALAuthorizationStatusNotDetermined = 0, 用户尚未做出了选择这个应用程序的问候
     ALAuthorizationStatusRestricted,        此应用程序没有被授权访问的照片数据。可能是家长控制权限。

ALAuthorizationStatusDenied,            用户已经明确否认了这一照片数据的应用程序访问.
     ALAuthorizationStatusAuthorized         用户已授权应用访问照片数据.
     }
     */
    int author = [ALAssetsLibrary authorizationStatus];
    NSLog(@"author type:%d",author);
    //关闭监听共享照片流产生的频繁通知信息
    [ALAssetsLibrary disableSharedPhotoStreamsSupport];
    
    //创建一个相册到相册资源中,并通过block返回创建成功的相册ALAssetsGroup
    [_library addAssetsGroupAlbumWithName:@"test" resultBlock:^(ALAssetsGroup *group) {
        //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]);
        groupURL = [group valueForProperty:ALAssetsGroupPropertyURL];

} failureBlock:^(NSError *error) {
        
    }];

新添加了一个名为test的相册

[NSThread sleepForTimeInterval:0.5];
    //通过url地址在相册资源中获取该地址的资源文件ALAsset,有可能是相片或视频
    [_library assetForURL:[NSURL URLWithString:@""] resultBlock:^(ALAsset *asset) {
        /*
         NSString *const ALAssetPropertyType;
         NSString *const ALAssetPropertyLocation;
         NSString *const ALAssetPropertyDuration;
         NSString *const ALAssetPropertyOrientation;
         NSString *const ALAssetPropertyDate;
         NSString *const ALAssetPropertyRepresentations;
         NSString *const ALAssetPropertyURLs;
         NSString *const ALAssetPropertyAssetURL;
         */
        //查看资源的地理位置信息
        NSLog(@"ALAssetPropertyLocation:%@",[asset valueForProperty:ALAssetPropertyLocation]);
        //如果资源是视频,查看视频的时长
        NSLog(@"ALAssetPropertyDuration:%@",[asset valueForProperty:ALAssetPropertyDuration]);
        //查看资源的方向,图片的旋转方向
        NSLog(@"ALAssetPropertyOrientation:%@",[asset valueForProperty:ALAssetPropertyOrientation]);
        //查看资源的创建时间
        NSLog(@"ALAssetPropertyDate:%@",[asset valueForProperty:ALAssetPropertyDate]);
        //查看资源的描述信息
        NSLog(@"ALAssetPropertyRepresentations:%@",[asset valueForProperty:ALAssetPropertyRepresentations]);
        NSLog(@"ALAssetPropertyURLs:%@",[asset valueForProperty:ALAssetPropertyURLs]);
        //查看资源的url路径
        NSLog(@"ALAssetPropertyAssetURL:%@",[asset valueForProperty:ALAssetPropertyAssetURL]);
    } failureBlock:^(NSError *error) {
        
    }];
    //通过url地址获取相册资源中的一个相册
    [_library groupForURL:groupURL resultBlock:^(ALAssetsGroup *group) {
        NSLog(@"ALAssetsGroupPropertyName:%@",[group valueForProperty:ALAssetsGroupPropertyName]);
    } failureBlock:^(NSError *error) {
    
    }];
    //根据选择的类型遍历相册资源中的相对应类型的所有相册,其中stop行参是指针,表示是否停止迭代,当赋值为false则停止
    /*
     enum {
     ALAssetsGroupLibrary        = (1 << 0),
     ALAssetsGroupAlbum          = (1 << 1),
     ALAssetsGroupEvent          = (1 << 2),
     ALAssetsGroupFaces          = (1 << 3),
     ALAssetsGroupSavedPhotos    = (1 << 4),
     ALAssetsGroupPhotoStream    = (1 << 5),
     ALAssetsGroupAll            = 0xFFFFFFFF,
     };
     */
    [_library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        NSLog(@"group name:%@",[group valueForProperty:ALAssetsGroupPropertyName]);
    } failureBlock:^(NSError *error) {
        
    }];
    //保存图片到系统默认的相册中,使用nsdata的形式,并返回照片的url地址
    [_library writeImageDataToSavedPhotosAlbum:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
        
    }];
    //保存图片到系统默认的相册中,使用cgimageref的形式,并返回照片的url地址
    [_library writeImageToSavedPhotosAlbum:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
        
    }];
    
    //保存图片到系统默认的相册中,使用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);
    }];

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) {
            
        }];
    }];

ALAsset类代表相册中的每个资源文件,可以通过它获取资源文件的相关信息还能修改和新建资源文件,ALAssetRepresentation类代表相册中每个资源文件的详细信息,可以通过它获取资源的大小,名字,路径等详细信息。

//通过ALAsset获取相对应的资源,获取图片的等比缩略图,原图的等比缩略
                        CGImageRef ratioThum = [asset aspectRatioThumbnail];
                        //获取相片的缩略图,该缩略图是相册中每张照片的poster图
                        CGImageRef thum = [asset thumbnail];
                        UIImage* rti = [UIImage imageWithCGImage:ratioThum];
                        UIImage* ti = [UIImage imageWithCGImage:thum];
                        UIImageView* v1 = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100, 120, 200)];
                        v1.image = rti;
                        v1.contentMode = UIViewContentModeScaleAspectFit;
                        UIImageView* v2 = [[UIImageView alloc]initWithFrame:CGRectMake(180, 100, 120, 200)];
                        v2.image = ti;
                        v2.contentMode = UIViewContentModeScaleAspectFit;
                        [self.view addSubview:v1];
                        [self.view addSubview:v2];

左侧为等比缩略图效果,右侧为poster效果的缩略图

UIImage* ni = [UIImage imageNamed:@"new.png"];
                        //修改指定路径的图片资源内容,替换掉原来的内容
                        [asset setImageData:UIImageJPEGRepresentation(ni, 1.0) metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
                            NSLog(@"new:%@",assetURL);
                        }];
                        //根据给定的图片内容,重新生成一张新图
                        [asset writeModifiedImageDataToSavedPhotosAlbum:UIImageJPEGRepresentation(ni, 1.0) metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
                            NSLog(@"new:%@",assetURL);
                        }];
                        //获取资源图片的详细资源信息
                        ALAssetRepresentation* representation = [asset defaultRepresentation];
                        //获取资源图片的长宽
                        CGSize dimension = [representation dimensions];
                        //获取资源图片的高清图
                        [representation fullResolutionImage];
                        //获取资源图片的全屏图
                        [representation fullScreenImage];
                        //获取资源图片的名字
                        NSString* filename = [representation filename];
                        NSLog(@"filename:%@",filename);
                        //缩放倍数
                        [representation scale];
                        //图片资源容量大小
                        [representation size];
                        //图片资源原数据
                        [representation metadata];
                        //旋转方向
                        [representation orientation];
                        //资源图片url地址,该地址和ALAsset通过ALAssetPropertyAssetURL获取的url地址是一样的
                        NSURL* url = [representation url];
                        NSLog(@"url:%@",url);
                        //资源图片uti,唯一标示符
                        NSLog(@"uti:%@",[representation UTI]);

自定义系统图片多选相册,类似于微信的在相册中选择多张照片的功能

AssetGroupViewController,用于显示系统有多少个相册给用户选择

AssetsViewController用于显示选择的相册中的所有图片资源,以collection的形式显示

AssetCollectionCell自定义cell,每个cell中定义需要显示的图片个数

CustomImageView用于自定义每个显示图片的view,添加点击事件用于通知选择了或取消选择了哪些图片

实现效果如下:

最新文章

  1. Java_Swing程序设计_尝试开发一个登陆窗体,包括用户名、密码以及提交按钮和重置按钮,当用户输入用户名my,密码love时,弹出登陆成功提示对话框。
  2. 再牛逼的梦想,也抵不住SB似的坚持
  3. hibernate用setResultTransformer转换
  4. [转]为什么不能用memcached存储Session
  5. Unity3d场景合并
  6. php resizeimage 部分jpg文件 生成缩略图失败
  7. 使用Visifire+ArcGIS API for Silverlight实现Graphic信息的动态图表显示
  8. javascript检测当前浏览器是否为微信浏览器
  9. Kafka Consumer
  10. letter combinations of a phone number(回溯)
  11. CentOS6.5 安装Python2.7后, yum出现“No module named yum”错误
  12. 用Docker解决坑爹的环境搭建系列——mysql:5.6
  13. 如何利用MongoDB实现高性能,高可用的双活应用架构?
  14. Win10问题汇总
  15. div轮流滚动显示
  16. vuejs2.0使用Sortable.js实现的拖拽功能
  17. 将文件夹下的所有csv文件存入数据库
  18. 奇怪吸引子---LorenzStenflo
  19. JAVA中数组Array与List互转
  20. LIbreOJ #6011. 「网络流 24 题」运输问题 最小费用最大流

热门文章

  1. BBR拥塞控制算法
  2. OpenSUSE 开启SSH 和网络设置
  3. biweb添加新的模块
  4. Windows英文版GitHub客户端使用操作流程图文攻略教程现没中文版
  5. centos 7.0 查看内存使用情况 和 查看硬盘使用情况
  6. PHPCMS V9 框架代码分析(入口程序)
  7. Mysql函数instr、locate、position VS like
  8. 水题 ZOJ 3880 Demacia of the Ancients
  9. HDU 5538 L - House Building 水题
  10. Zookeeper + Hadoop + Hbase部署备忘
  11. ashx文件结合ajax使用(返回json数据)
  12. 使用atomic一定是线程安全的吗
  13. java 吞吐量
  14. iPhone应用提交流程:如何将App程序发布到App Store?
  15. 【Demo 0015】位置服务及地图
  16. linux下登陆MongoDB的两种方式
  17. thoughtworks面试题分析与解答
  18. 今天我点亮了CSDN博客专家殊荣
  19. spring的DI.IoC是什么
  20. HTML第一耍 标题 段落 字体等标签