您的位置:首页 > 移动开发 > IOS开发

oc获取相册源码

2015-09-07 13:26 447 查看
dispatch_async(dispatch_get_main_queue(), ^{

       

        @autoreleasepool {

        

            ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *err){

                if ([err.localizedDescription rangeOfString:@"Global denied access"].location!=NSNotFound) {

                    [SGMessage showMsg:@"无法访问相册.请在'设置->定位服务'设置为打开状态." confirmTitle:@"确定" delegate:nil];

                }else{

                    [SGNotice showErr:@"相册访问失败"];

                }

            };

            ALAssetsGroupEnumerationResultsBlock groupEnumerAtion = ^(ALAsset *result,NSUInteger index, BOOL *stop){

                if (result!=NULL) {

                    if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {

                        // 完整的路径

                        NSString *urlstr=[NSString stringWithFormat:@"%@",result.defaultRepresentation.url];//图片的url

                        // 大图

                        // UIImage *image = [UIImage imageWithCGImage:result.defaultRepresentation.fullScreenImage];

                        // 缩略图

                        UIImage *thumbImage = [UIImage imageWithCGImage:result.thumbnail];

                        

                        // 完整名称

                        // NSString *resultName= [[urlstr substringFromIndex:[urlstr rangeOfString:@"id="].location+3] stringByReplacingOccurrencesOfString:@"&ext=" withString:@"."];

                        

                        [self.urlStringData addObject:urlstr];

                    }

                }

            };

            ALAssetsLibraryGroupsEnumerationResultsBlock libraryGroupsEnumeration = ^(ALAssetsGroup* group,BOOL* stop){

                if (group!=nil) {

                    

                    // 取得分组名称

                    NSString *f = [NSString stringWithFormat:@"%@",group];//获取相簿的组

                    NSString *s = [f substringFromIndex:16 ];

                    NSArray *arr = [NSArray arrayWithArray:[s componentsSeparatedByString:@","]];

                    

                    NSString *groupName = [[arr objectAtIndex:0]substringFromIndex:5];

   

                    

                    NSLog(@"---%@", groupName);

                    

                    if ([groupName isEqualToString:@"My Photo Stream"] ) {

                        [group enumerateAssetsUsingBlock:groupEnumerAtion];

                    }

                    

                }

            };

            

            ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];

            [library enumerateGroupsWithTypes:ALAssetsGroupAll

                                  usingBlock:libraryGroupsEnumeration

                                failureBlock:failureblock];

                    

        }

        

    });
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS