码迷,mamicode.com
首页 > 其他好文 > 详细

获取某一个相册中所有照片和照片的缩略图

时间:2014-06-05 12:06:55      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:相册



- (void)loadAssets {
    
    // Initialise
    _assets = [NSMutableArray new];
    _assetLibrary = [[ALAssetsLibrary alloc] init];
    
    // Run in the background as it takes a while to get all assets from the library
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
        NSMutableArray *assetURLDictionaries = [[NSMutableArray alloc] init];
        
        // Process assets
        void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
            if (result != nil) {
                if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
                    [assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];
                    NSURL *url = result.defaultRepresentation.url;
                    [_assetLibrary assetForURL:url
                                   resultBlock:^(ALAsset *asset) {
                                       if (asset) {
                                           @synchronized(_assets) {
                                               [_assets addObject:asset];
                                               if (_assets.count == 1) {
                                                   // Added first asset so reload data
                                                   //                                                   [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
                                               }
                                           }
                                       }
                                       
                                   }
                                  failureBlock:^(NSError *error){
                                      NSLog(@"operation was not successfull!");
                                  }];
                    
                }
            }
        };
        
        
        ALAssetsLibraryAccessFailureBlock failureblock =
        ^(NSError *myerror)
        {
            NSLog(@"相册访问失败 =%@", [myerror localizedDescription]);
            if ([myerror.localizedDescription rangeOfString:@"Global denied access"].location!=NSNotFound) {
                NSLog(@"无法访问相册.请在‘设置->定位服务‘设置为打开状态.");
            }else{
                NSLog(@"相册访问失败.");
            }
        };
        
        
        // Process groups
        void (^ assetGroupEnumerator) (ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) {
            if (group != nil) {
                NSString *g=[NSString stringWithFormat:@"%@",group];//获取相簿的组
                NSString *g1=[g substringFromIndex:16] ;
                NSArray *arr=[[NSArray alloc] init];
                arr=[g1 componentsSeparatedByString:@","];
                NSString *g2=[[arr objectAtIndex:0] substringFromIndex:5];
                if ([g2 isEqualToString:@"Camera Roll"]) {
                    g2=@"相机胶卷";
                }
                if ([g2 isEqualToString:@"MyPhoto"]) {
                    *stop = NO;
                    [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:assetEnumerator];
                    [assetGroups addObject:group];
                }
                
            }else{
                NSLog(@"********else!");
                NSMutableArray *photos = [[NSMutableArray alloc] init];
                NSMutableArray *thumbs = [[NSMutableArray alloc] init];
                @synchronized(_assets) {
                    NSMutableArray *copy = [_assets copy];
                    for (ALAsset *asset in copy) {
                        [photos addObject:[MWPhoto photoWithURL:asset.defaultRepresentation.url]];
                        [thumbs addObject:[MWPhoto photoWithImage:[UIImage imageWithCGImage:asset.thumbnail]]];
                    }
                }
                self.photos = photos;//照片
                self.thumbs = thumbs;//缩略图
            }
        };
        
        // Process!
        [self.assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll
                                         usingBlock:assetGroupEnumerator
                                       failureBlock:failureblock];
    });
    

}


技术交流园地,求职->

欢迎大家关注微信公众账号:iOSDevTip【直接扫描下面二维码即可】

bubuko.com,布布扣

获取某一个相册中所有照片和照片的缩略图,布布扣,bubuko.com

获取某一个相册中所有照片和照片的缩略图

标签:相册

原文地址:http://blog.csdn.net/iosdevtip/article/details/27355735

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!