首页 > 代码库 > 获取某一个相册中所有照片和照片的缩略图

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



- (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【直接扫描下面二维码即可】