2016-08-04 47 views
0

我在故事板UITableView中創建了原型單元格,TableViewController和UITableViewCell。現在我想選擇手機圖庫中的所有圖片並將一個或多個圖片保存到數據庫中。但在這一刻,我需要實現添加圖片到tableview。我是iOS的初學者。誰能告訴我怎麼能做到這一點?給我一個鏈接或代碼?感謝您的提前如何將圖像添加到手機圖庫中的UITableView

+0

試試這個http://stackoverflow.com/questions/12633843/get-all -of-the-pictures-from-an-iphone-photolibrary-in-an-an-an-array-using-assetslibr – sanjeet

+0

kenzolek檢查我的回答 – user3182143

+0

我的回答對你有幫助嗎? – user3182143

回答

2

您可以使用ALAssetsLibraryAssetsLibrary framework從手機庫中獲取所有圖像。將每個圖像保存到一個數組中。

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

void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) { 

    if(result != nil) { 

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

      NSURL *url= (NSURL*) [[result defaultRepresentation] url]; 
      [library assetForURL:url 
        resultBlock:^(ALAsset *asset) { 
         UIImage *img = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]; 
         [imgArray addbject:img]; 
        } 
        failureBlock:^(NSError *error){ NSLog(@"operation failed"); } ]; 
     } 
    } 
}; 

然後,您可以使用此數組保存到數據庫或在tableview中顯示。 對於在TableView中使用,您需要在原型單元格中添加UIImageView,然後相應地將陣列中的圖像設置爲單元格。您的表格視圖委託方法將如下所示。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [imgArray count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    CellId = @"ImageCell"; 
    CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellId]; 
    if (cell == nil) { 
     cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellId]; 
    } 
    UIImage *img = [imgArray objectAtIndex:indexPath.row]; 
    cell.imgView.image = img; 
    return cell; 
} 
1

如果您想選擇從圖庫中的圖像,並將其展示給tableview中,你可以使用自定義的ImageView中的tableView或CustomCell ImageView的

首先,你需要從圖庫中選擇圖像並將其保存到數組

該分配和初始化在viewDidLoad方法陣列

ViewController.m

#import "ViewController.h" 

@interface ViewController() 
{ 
    NSMutableArray *arrayImage; 
} 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
     [super viewDidLoad]; 
     arrayImage = [[NSMutableArray alloc]init]; 
} 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    UIImage *image=[info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
    imageView.image=image; 
    [arrayImage addObject:image]; 
    picker.delegate =self; 
    [picker dismissViewControllerAnimated:YES completion:nil]; 
} 
之前

然後在的tableView

如果CustomCell

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    if(cell==nil) 
    { 
    NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"CustomTableViewCell" owner:self options:nil]; 
    cell = nib[0]; 
    } 
cell.galleryImageView.image = [arrayImage objectAtIndex:indexPath.row]; 
return cell; 
} 

如果使用默認表視圖細胞

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *strCell = @"cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCell]; 
    if (cell == nil) 
    { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell]; 
    } 
    cell.imageView.image = [arrayImage objectAtIndex:indexPath.row]; 
    return cell; 
}