2012-04-16 131 views
4

我想在照片庫視圖中顯示照片庫中的所有圖片。我能夠通過assetsLibrary訪問所有圖像,但無法在表格中顯示它們。我沒有收到任何錯誤,但仍然不知道發生了什麼。從iphone照片庫中獲取照片並在表格視圖中顯示

NSMutableArray *assets = [[NSMutableArray alloc]init]; 

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

[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop){ 

     if(group != NULL){ 

     [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop){ 

       if(result != NULL){ 
       [assets addObject:result]; 
       }else NSLog(@"NO photo");; 
      }]; 
    } 
} 


failureBlock:^(NSError *error){NSLog(@"Error");}]; 

爲表視圖的數據源方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

NSString *identifier = @"id"; 

UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:identifier]; 

if(cell == NULL){ 

    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 
} 

[cell.imageView setImage:[UIImage imageWithCGImage:[[assets objectAtIndex:indexPath.row]thumbnail]]]; 

[cell textLabel].text = @"Photo"; 

return cell; 

}

Pleaes幫助我在做什麼錯了....

感謝您的幫助

回答

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

對象從庫實例找回壽命是綁庫的壽命instance.I添加一個靜態方法來檢索類的共享實例。

+ (ALAssetsLibrary *)defaultAssetsLibrary { 

    static dispatch_once_t pred = 0; 

    static ALAssetsLibrary *library = nil; 

    dispatch_once(&pred, ^{ 

    library = [[ALAssetsLibrary alloc] init]; 

    }); 

    return library; 
} 
0
NSMutableArray *assets = [[NSMutableArray alloc]init]; 

在上面的代碼添加__block如下:

__block NSMutableArray *assets = [[NSMutableArray alloc]init];