#define kWidth (self.view.bounds.size.width) //界面宽度
#define kHeight (self.view.bounds.size.height)//高度
@interface ViewController (){
NSMutableSet *imageMutSet;//相册集合
NSMutableArray *imageThumbArry;//缩略图数组
}
imageMutSet = [NSMutableSet setWithCapacity:3];//记录当前手机所有的相册
imageThumbArry = [NSMutableArray arrayWithCapacity:40];//图片缩略图数组
ALAssetsLibrary *assetsLibrary;
assetsLibrary = [[ALAssetsLibrary alloc] init];
NSMutableArray *groupArray;
groupArray = [[NSMutableArray alloc] initWithCapacity:1];
//遍历相册数组 和 数组中的图片
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
[groupArray addObject:group];//添加元素
//可以知道相册的名字,从而知道安装的部分应用,例如 Name:柚子相机, Type:Album, Assets count:1
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
//将相册中的缩略图放入视图上
UIImageView *imgV = [[UIImageView alloc] initWithFrame:
CGRectMake(index%6*kWidth/6, 64+index/6*kHeight/9, kWidth/6, kHeight/9)];
imgV.contentMode = UIViewContentModeScaleAspectFit;
//NSString *type = [result valueForProperty:ALAssetPropertyType];
if (result) {//图片视图的循环创建
[imageMutSet addObject:result];//元素是无序的
[imageThumbArry addObject:[UIImage imageWithCGImage:result.thumbnail]];
imgV.image = imageThumbArry[index];//视图填充缩略图
[self.view addSubview:imgV];//NSLog(@"result ==== %@",result);
}];
NSLog(@"thumbAry ==== %@",imageThumbArry);
}
}
failureBlock:^(NSError *error) {
UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"找不到相册"
message:@"失败语块的警示"
delegate:self
cancelButtonTitle:@"好吧,不弄了"
otherButtonTitles:@"不中,就是怼", nil];
[alertV show];//NSLog(@"Group not found!\n");
}];
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{手势做相应的操作
/*
http://my.oschina.net/u/1378445/blog/333052
ALAssetsGroup就是相册的类,可以通过valueForProperty方法查看不同属性的值,如:ALAssetsGroupPropertyName,相册名。
ALAssetsGroup类有几个方法
posterImage方法就是相册的封面图片,numberOfAssets方法获取该相册的图片视频数量,可以通过enumerateAssetsUsingBlock方法列举出所有照片。
ALAssetsGroup
可以使用setAssetsFilter:(ALAssetsFilter *)filter过滤照片或者视频等
*/
ALAssetsLibrary *assetsLib = [[ALAssetsLibrary alloc] init];//相册集合
NSMutableArray *groupAry = [NSMutableArray arrayWithCapacity:1];
[assetsLib enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) [groupAry addObject:group];
}
failureBlock:^(NSError *error) {
NSLog(@"Group not found!\n");
}];
//ALAsset类也可以通过valueForProperty方法查看不同属性的值,
//如:ALAssetPropertyType,asset的类型,有三种ALAssetTypePhoto, ALAssetTypeVideo or ALAssetTypeUnknown。
//2】另外还可以通过该方法获取ALAssetPropertyLocation(照片位置),ALAssetPropertyDuration(视频时间),ALAssetPropertyDate(照片拍摄日期)等。
//可以通过thumbnail方法就是获取该照片。根据相册获取该相册下所有图片,通过ALAssetsGroup的实例方法得到ALAsset类数组。
//3】ALAsset类有一个defaultRepresentation方法,返回值是ALAssetRepresentation类,该类的作用就是获取该资源图片的详细资源信息
//1)获取资源图片的详细资源信息ALAssetRepresentation *representation = [asset defaultRepresentation];
//2)获取资源图片的长宽CGSize dimension = [representation dimensions];
//3)获取资源图片的高清图[representation fullResolutionImage];
//4)获取资源图片的全屏图[representation fullScreenImage];
//5)获取资源图片的名字NSString *filename = [representation filename];//NSLog(@"filename:%@",filename);
//6)缩放倍数[representation scale];
//7)图片资源容量大小[representation size];
//8)图片资源原数据[representation metadata];
//9)旋转方向[representation orientation];
//10)资源图片url地址,该地址和ALAsset通过ALAssetPropertyAssetURL获取的url地址是一样的
//NSLog(@"url:%@",url);NSURL *url = [representation url];
//11)资源图片uti,唯一标示符NSLog(@"uti:%@",[representation UTI]);
联系客服