iOS 获取系统相册全部照片以及照片信息
来源:互联网 发布:淘宝上真正的原单店 编辑:程序博客网 时间:2024/04/29 09:37
感谢:smile筱提供了参考代码传输门。
本文是在他的代码的基础上的进一步修改。
写下本博客主要是为了自己备份知识。方便查找。转载请注明出处!
具体实现:
#import <AssetsLibrary/AssetsLibrary.h>
优化一:
-(void)getImgs{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ __block int photoCount = 0; ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror){ NSLog(@"相册访问失败 =%@", [myerror localizedDescription]); if ([myerror.localizedDescription rangeOfString:@"Global denied access"].location!=NSNotFound) { NSLog(@"无法访问相册.请在'设置->定位服务'设置为打开状态."); }else{ NSLog(@"相册访问失败."); } }; ALAssetsGroupEnumerationResultsBlock groupEnumerAtion = ^(ALAsset *result,NSUInteger index, BOOL *stop){ if (result!=NULL) { if ([[result valueForProperty:ALAssetPropertyType]isEqualToString:ALAssetTypePhoto]) { NSString *photoDefaultPath=[NSString stringWithFormat:@"%@",result.defaultRepresentation.url]; NSString *photoThumPath=[NSString stringWithFormat:@"%@",result.defaultRepresentation.url]; NSString *photoName=[NSString stringWithFormat:@"%@",result.defaultRepresentation.filename]; NSDictionary *photoMetaData = result.defaultRepresentation.metadata; NSDictionary *photoGPS = [photoMetaData objectForKey:@"{GPS}"]; if (photoGPS == nil) { photoGPS = @{@"Latitude":@"0.0",@"Longitude":@"0.0"}; } NSString *photoDateTime; if ([[photoMetaData objectForKey:@"{TIFF}"] objectForKey:@"DateTime"] == nil) { photoDateTime = @"0"; }else{ photoDateTime = [[photoMetaData objectForKey:@"{TIFF}"] objectForKey:@"DateTime"]; } //图片的url /*result.defaultRepresentation.fullScreenImage//图片的大图 result.thumbnail //图片的缩略图小图 // NSRange range1=[urlstr rangeOfString:@"id="]; // NSString *resultName=[urlstr substringFromIndex:range1.location+3]; // resultName=[resultName stringByReplacingOccurrencesOfString:@"&ext=" withString:@"."];//格式demo:123456.png */ NSDictionary *photoDic = @{@"photoDefaultPath":photoDefaultPath,@"photoThumPath":photoThumPath,@"photoName":photoName,@"photoGPS":photoGPS,@"photoDateTime":photoDateTime}; NSLog(@"photoDic:%@",photoDic); [_systemPhotoArray addObject:photoDic]; NSLog(@"加载全部照片共%d张中的第%d张",photoCount,index + 1); if (photoCount == index + 1) { NSLog(@"全部加载完毕"); } } } }; ALAssetsLibraryGroupsEnumerationResultsBlock libraryGroupsEnumeration = ^(ALAssetsGroup* group,BOOL* stop){ if (group!=nil) { NSString *g=[NSString stringWithFormat:@"%@",group];//获取相簿的组 NSLog(@"gg:%@",g);//gg:ALAssetsGroup - Name:Camera Roll, Type:Saved Photos, Assets count:71 NSString *g1=[g substringFromIndex:16 ] ; NSArray *arr=[NSArray arrayWithArray:[g1 componentsSeparatedByString:@","]]; NSString *g2=[[arr objectAtIndex:0]substringFromIndex:5]; if ([g2 isEqualToString:@"Camera Roll"]) { g2=@"相机胶卷"; }// NSString *groupName=g2;//组的name photoCount = group.numberOfAssets; [group enumerateAssetsUsingBlock:groupEnumerAtion]; }else{ } }; ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init]; [library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:libraryGroupsEnumeration failureBlock:failureblock]; });}
优化二(使用block回调):
+ (void)SystemPhotoAlbumPhotoSuccess:(void(^)(NSArray *photoArray))success progress:(void(^)(int currentCount,int totalCount))progress fail:(void(^)(NSString *error))fail{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ __block int photoCount = 0; __block NSMutableArray *phArray = [NSMutableArray array]; ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror){ dispatch_async(dispatch_get_main_queue(), ^{ if (fail) { fail([myerror localizedDescription]); } NSLog(@"相册访问失败 =%@", [myerror localizedDescription]); if ([myerror.localizedDescription rangeOfString:@"Global denied access"].location!=NSNotFound) { NSLog(@"无法访问相册.请在'设置->定位服务'设置为打开状态."); }else{ NSLog(@"相册访问失败."); } }); }; ALAssetsGroupEnumerationResultsBlock groupEnumerAtion = ^(ALAsset *result,NSUInteger index, BOOL *stop){ if (result!=NULL) { if ([[result valueForProperty:ALAssetPropertyType]isEqualToString:ALAssetTypePhoto]) { NSString *photoDefaultPath=[NSString stringWithFormat:@"%@",result.defaultRepresentation.url]; NSString *photoThumPath=[NSString stringWithFormat:@"%@",result.defaultRepresentation.url]; NSString *photoName=[NSString stringWithFormat:@"%@",result.defaultRepresentation.filename]; NSDictionary *photoMetaData = result.defaultRepresentation.metadata; NSDictionary *photoGPS = [photoMetaData objectForKey:@"{GPS}"]; if (photoGPS == nil) { photoGPS = @{@"Latitude":@"0.0",@"Longitude":@"0.0"}; } NSString *photoDateTime; if ([[photoMetaData objectForKey:@"{TIFF}"] objectForKey:@"DateTime"] == nil) { photoDateTime = @"0"; }else{ photoDateTime = [[photoMetaData objectForKey:@"{TIFF}"] objectForKey:@"DateTime"]; } //图片的url /*result.defaultRepresentation.fullScreenImage//图片的大图 result.thumbnail //图片的缩略图小图 // NSRange range1=[urlstr rangeOfString:@"id="]; // NSString *resultName=[urlstr substringFromIndex:range1.location+3]; // resultName=[resultName stringByReplacingOccurrencesOfString:@"&ext=" withString:@"."];//格式demo:123456.png */ NSDictionary *photoDic = @{@"photoDefaultPath":photoDefaultPath,@"photoThumPath":photoThumPath,@"photoName":photoName,@"photoGPS":photoGPS,@"photoDateTime":photoDateTime}; // NSLog(@"photoDic:%@",photoDic); [phArray addObject:photoDic]; // NSLog(@"加载全部照片共%d张中的第%d张",photoCount,index + 1); dispatch_async(dispatch_get_main_queue(), ^{ if (progress) { progress(index +1 , photoCount); } }); if (photoCount == index + 1) {// NSLog(@"全部加载完毕"); dispatch_async(dispatch_get_main_queue(), ^{ if (success) { success(phArray); } }); } } } }; ALAssetsLibraryGroupsEnumerationResultsBlock libraryGroupsEnumeration = ^(ALAssetsGroup* group,BOOL* stop){ if (group!=nil) { NSString *g=[NSString stringWithFormat:@"%@",group];//获取相簿的组// NSLog(@"gg:%@",g);//gg:ALAssetsGroup - Name:Camera Roll, Type:Saved Photos, Assets count:71 NSString *g1=[g substringFromIndex:16 ] ; NSArray *arr=[NSArray arrayWithArray:[g1 componentsSeparatedByString:@","]]; NSString *g2=[[arr objectAtIndex:0]substringFromIndex:5]; if ([g2 isEqualToString:@"Camera Roll"]) { g2=@"相机胶卷"; } // NSString *groupName=g2;//组的name photoCount = group.numberOfAssets; [group enumerateAssetsUsingBlock:groupEnumerAtion]; }else{ } }; ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init]; [library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:libraryGroupsEnumeration failureBlock:failureblock]; });}
下面是调用实例(ToolsManager是我的一个工具类,我将此方法写进工具类作为类方法调用):
[ToolsManager SystemPhotoAlbumPhotoSuccess:^(NSArray *photoArray) { NSLog(@"photoArray.count:%d",photoArray.count); } progress:^(int currentCount, int totalCount) { CGFloat percent = (CGFloat)currentCount/(CGFloat)totalCount; _photoAlbumBtn.text = [NSString stringWithFormat:@"载入照片中(%d%%)...",(int)(percent * 100)]; _photoAlbumBtn.progress = percent; } fail:^(NSString *error) { UIAlertView *av; if ([error rangeOfString:@"Global denied access"].location!=NSNotFound) { av = [[UIAlertView alloc] initWithTitle:@"错误" message:@"无法访问相册.请在'设置->定位服务'设置为打开状态." delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil]; }else{ av = [[UIAlertView alloc] initWithTitle:@"错误" message:@"相册访问失败." delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil]; } [av show]; }];
上面是获取照片URL的。
根据照片URL可以用一下代码来反向获取到UIImage
//------------------------根据图片的url反取图片----- ALAssetsLibrary *assetLibrary=[[ALAssetsLibraryalloc] init]; NSURL *url=[NSURLURLWithString:urlStr];[assetLibrary assetForURL:url resultBlock:^(ALAsset *asset) { UIImage *image=[UIImage imageWithCGImage:asset.thumbnail]; cellImageView.image=image; }failureBlock:^(NSError *error) { NSLog(@"error=%@",error); } ];//---------------------
0 0
- iOS 获取系统相册全部照片以及照片信息
- iOS - 获取系统相册照片名称,路径以及各项信息
- ios 获取全部照片
- 获取IOS系统相册中的照片
- iOS开发:调用系统自带相机以及获取相册照片的功能实现
- iOS 开发调用相机以及获取相册照片功能
- iOS 开发调用相机以及获取相册照片功能
- ios 获取系统照片
- Android 获取本地相册照片信息
- iOS获取照片EXif信息
- 获取系统相册照片(直接提取照片)
- 在S5PV210平台上实现二代身份证解码,安卓系统,可以获取全部文字信息以及电子照片
- IOS从相册获取裁剪的照片
- IOS获取相册照片的大小
- 获取系统相册里面的照片
- 获取相册中的照片
- 获取相册照片和获取拍照照片
- iOS 上传照片到服务器 获取相册和拍照功能的照片 照片压缩上传
- Android-IntentService
- Windows 7程序开发系列之二(JumpList篇2 - Destination)
- Linux应用程序学习之制作静态函数库
- 记录一个JS异常Uncaught NotFoundError
- Android-BoundService
- iOS 获取系统相册全部照片以及照片信息
- IOS银联支付的流程
- R语言离线安装二进制包的几个问题
- android百度地图自定义覆盖物
- HDU 4283 You Are the One(区间DP)
- Android-Service通知与线程
- selenium登录脚本
- 為ListView的Item設置不同的布局.
- R语言将源码包编译为二进制文件