关于获取 手机相片的帖子一堆。但是里面很多的内容都是互相copy,有些方法写的不清楚,我在这里给大家完善下,代码如下:
dispatch_async(dispatch_get_main_queue(), ^{ @autoreleasepool { ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError *error) { NSLog(@"相册访问失败 = %@", [error localizedDescription]); if ([error.localizedDescription rangeOfString:@"Global denied access"].location!=NSNotFound) { NSLog(@"无法访问相册.请在'设置->定位服务'设置为打开状态."); } else { NSLog(@"相册访问失败."); } }; ALAssetsGroupEnumerationResultsBlock groupBlock = ^(ALAsset *result, NSUInteger index, BOOL *stop) { if (result!=NULL) { if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) { NSLog(@"index = %d",index); // NSString *urlStr=[NSString stringWithFormat:@"%@",result.defaultRepresentation.url];//图片的url UIImage *img=[UIImage imageWithCGImage:result.thumbnail]; [photoArr addObject:img]; } } }; ALAssetsLibraryGroupsEnumerationResultsBlock libraryGroupBlock = ^(ALAssetsGroup* group, BOOL* stop) { NSLog(@"stop = %d",stop?1:0); if (group == nil) { NSLog(@"group == nil 结束"); } if (group!=nil) { NSString *g=[NSString stringWithFormat:@"%@",group]; NSLog(@"%@",g); [group enumerateAssetsUsingBlock:groupBlock]; } }; ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init]; [library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:libraryGroupBlock failureBlock:failureBlock]; } });
打印结果:
>> stop = 1
>> ALAssetsGroup - Name:QQ, Type:Album, Assets count:0
>> stop = 1
>> ALAssetsGroup - Name:Camera Roll, Type:Saved Photos, Assets count:65
>> index = 0
.
.
.
>> index = 64
>> stop = 1
>> ALAssetsGroup - Name:My Photo Stream, Type:Photo Stream, Assets count:772
>> index = 0
.
.
.
>> index = 771
>> stop = 1
>> group == nil 结束
以上为全部的日志。
其实代码很简单,就是几个block块的定义和实现,按照如下步骤执行。
1、先弹出是否允许访问相册。点击允许后
2、执行 libraryGroupBlock(可以截取和分割字符串,获取相册相关信息)
3、执行 groupBlock (该相册的照片信息,index是从0开始的。)
4、再执行 libraryGroupBlock(有多少本相册,执行多少次)
5、再执行 groupBlock
6、一直到 >> group == nil 的时候,才算结束。
注:
700多张相片,存在集合里面(缩略图)
内存变化:(执行前)
内存变化:(执行后)
时间变化:(700多张相片)
第一次点击,450ms,第二次 250ms ,第三次 250ms
去掉:
UIImage *img=[UIImage imageWithCGImage:result.thumbnail]; [photoArr addObject:img];
第一次点击,400ms,第二次 200ms ,第三次 200ms
照片中还有一起其他的属性如下:
//唯一ID? NSString *uti = result.defaultRepresentation.UTI; NSLog(@"uti = %@",uti); //图片的长宽 CGSize dimensions = result.defaultRepresentation.dimensions; NSLog(@"width = %f . height = %f ",dimensions.width,dimensions.height); //图片的全屏图 CGImageRef refScreen = result.defaultRepresentation.fullScreenImage; //图片的高清图 CGImageRef refResolution = result.defaultRepresentation.fullResolutionImage; //图片名称 NSString *fileName = result.defaultRepresentation.filename; NSLog(@"fileName = %@",fileName); //图片大小 long long size = result.defaultRepresentation.size; NSLog(@"size = %lld",size); //图片原数据 NSDictionary *metadata = result.defaultRepresentation.metadata; NSLog(@"metadata = %@",metadata); //图片方向 ALAssetOrientation orientation = result.defaultRepresentation.orientation; NSLog(@"orientation = %d",orientation); //图片缩放大小 float scale = result.defaultRepresentation.scale; NSLog(@"scale = %f",scale); //图片URL NSURL *url = result.defaultRepresentation.url; NSLog(@"url = %@",url);
输出:
>> uti = public.jpeg(不知道UTI什么意思,所有图片都是一样)
>> width = 1260.000000 . height = 1680.000000
>> fileName = IMG_0670.JPG
>> size = 543520
>> metadata = {
ColorModel = RGB;
DPIHeight = 72;
DPIWidth = 72;
Depth = 8;
Orientation = 1;
PixelHeight = 1680;
PixelWidth = 1260;
"{Exif}" = {
ColorSpace = 1;
ComponentsConfiguration = (
1,
2,
3,
0
);
ExifVersion = (
2,
2,
1
);
FlashPixVersion = (
1,
0
);
PixelXDimension = 1260;
PixelYDimension = 1680;
SceneCaptureType = 0;
};
"{TIFF}" = {
Orientation = 1;
ResolutionUnit = 2;
XResolution = 72;
YResolution = 72;
};
}
>> orientation = 0
>> scale = 1.000000
>> url = assets-library://asset/asset.JPG?id=9801003C-5700-4670-B255-FCD27297B74A&ext=JPG
参考:
http://www.tuicool.com/articles/UBZJFb