2016-08-04 2 views
0

프로토 타입 셀 TableviewController와 UITableViewCell으로 스토리 보드 UITableView에서 만들었습니다. 이제 전화 갤러리에서 모든 사진을 선택하고 데이터베이스에 하나 이상을 저장하려고합니다. 그러나이 순간 나는 tableview에 그림을 추가하는 것을 구현해야합니다. 나는 iOS에서 초보자입니다. 아무도 저에게 어떻게 할 수 있다고 말할 수 있습니까? 나에게 링크 나 코드를 주시겠습니까? 사전에 보내 주셔서 감사합니다.전화 갤러리에서 UITableView에 이미지를 추가하는 방법

+0

를 사용하는 경우

-(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; } 

를 CustomCell 경우,이 http://stackoverflow.com/questions/12633843/get-all -of-the-pictures-from-an-iphone-photolibrary-in-an-array-using-assetslibr – sanjeet

+0

kenzolek 내 대답 확인 – user3182143

+0

내 대답은 도움이됩니까? – user3182143

답변

2

AssetsLibrary framework에서 ALAssetsLibrary을 사용하면 전화 갤러리에서 모든 이미지를 가져올 수 있습니다. 각 이미지를 배열에 저장하십시오.

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"); } ]; 
     } 
    } 
}; 

그런 다음이 배열을 사용하여 DB에 저장하거나 tableview에 표시 할 수 있습니다. TableView에서 사용하려면 프로토 타입 셀에 UIImageView을 추가 한 다음 배열의 이미지를 그에 따라 셀로 설정해야합니다. 테이블 뷰 대리자 메소드는 아래와 같이됩니다. 당신은 갤러리에서 이미지를 선택하는 원하고있는 tableview에게 보여 경우

- (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 또는 CustomCell 이미지 뷰

처음에 사용자 정의 이미지 뷰를 사용할 수 있습니다 그 할당하고있는 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]; 
} 
,174 전에

그리고있는 tableView에서

당신이 시도 기본 테이블 뷰 셀에게

- (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; 
} 
관련 문제