将捕获的图像存储在数组中?

时间:2012-09-25 23:39:35

标签: uitableview uiimagepickercontroller

我到处都试图找到这个问题的答案,我有一个相机屏幕,你拍照片然后需要将照片存储在某处,然后可以从桌面视图中访问。

1 个答案:

答案 0 :(得分:2)

UIImagePickerController中捕获的图像存储在NSArray中有效。

你可以这样:

/* ViewController.h */
@interface ViewController : UIViewController <UIImagePickerControllerDelegate>

@property (nonatomic, strong) UIImagePickerController *imagePicker;
@property (nonatomic, strong) NSMutableArray *photos;

/* ViewController.m */
@synthesize imagePicker = _imagePicker;
@synthesize photos = _photos;

// initialize imagePicker
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
self.imagePicker = imagePicker;

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self.photos addObject:selectedImage];
}

编辑:要在表格视图中查看数组中的图像,您可以这样:

// I'm assuming you only have 1 section for the table view

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.photos count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // get / instantiate cell

    // This will use the default imageView in a UITableViewCell.
    cell.imageView.image = [self.photos objectAtIndex:indexPath.row];
}