iOS遍历相册中的图片
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS遍历相册中的图片相关的知识,希望对你有一定的参考价值。
1 //获取相册的所有图片 2 - (void)reloadImagesFromLibrary 3 { 4 self.images = [[NSMutableArray alloc] init]; 5 dispatch_async(dispatch_get_main_queue(), ^{ 6 7 @autoreleasepool { 8 ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror){ 9 NSLog(@"相册访问失败 =%@", [myerror localizedDescription]); 10 if ([myerror.localizedDescription rangeOfString:@"Global denied access"].location!=NSNotFound) { 11 NSLog(@"无法访问相册.请在‘设置->定位服务‘设置为打开状态."); 12 }else{ 13 NSLog(@"相册访问失败."); 14 } 15 }; 16 17 ALAssetsGroupEnumerationResultsBlock groupEnumerAtion = ^(ALAsset *result, NSUInteger index, BOOL *stop){ 18 if (result!=NULL) { 19 20 if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) { 21 22 NSString *urlstr=[NSString stringWithFormat:@"%@",result.defaultRepresentation.url];//图片的url 23 [self.images addObject:urlstr]; 24 //NSLog(@"urlStr is %@",urlstr); 25 //result.defaultRepresentation.fullScreenImage//图片的大图 26 //result.thumbnail //图片的缩略图小图 27 NSRange range1=[urlstr rangeOfString:@"id="]; 28 NSString *resultName=[urlstr substringFromIndex:range1.location+3]; 29 resultName=[resultName stringByReplacingOccurrencesOfString:@"&ext=" withString:@"."];//格式demo:123456.png 30 31 } 32 } 33 }; 34 35 ALAssetsLibraryGroupsEnumerationResultsBlock libraryGroupsEnumeration = ^(ALAssetsGroup* group, BOOL* stop){ 36 37 if (group == nil) 38 { 39 40 } 41 42 if (group!=nil) { 43 NSString *g=[NSString stringWithFormat:@"%@",group];//获取相簿的组 44 NSLog(@"gg:%@",g);//gg:ALAssetsGroup - Name:Camera Roll, Type:Saved Photos, Assets count:71 45 46 NSString *g1=[g substringFromIndex:16 ] ; 47 NSArray *arr=[[NSArray alloc] init]; 48 arr=[g1 componentsSeparatedByString:@","]; 49 NSString *g2=[[arr objectAtIndex:0] substringFromIndex:5]; 50 if ([g2 isEqualToString:@"Camera Roll"]) { 51 g2=@"相机胶卷"; 52 } 53 NSString *groupName=g2;//组的name 54 55 [group enumerateAssetsUsingBlock:groupEnumerAtion]; 56 } 57 58 }; 59 60 ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init]; 61 [library enumerateGroupsWithTypes:ALAssetsGroupAll 62 usingBlock:libraryGroupsEnumeration 63 failureBlock:failureblock]; 64 } 65 66 }); 67 }
以上是关于iOS遍历相册中的图片的主要内容,如果未能解决你的问题,请参考以下文章