0

我有一个应用程序,其上有一个 takePhoto UIButtonUIViewcontroller使用户能够使用imagePickerController. 然后,我希望将照片放置在UITableView.

照片部分有效,出于测试目的,我将图像显示在UIImage photoImageView.image

我添加了UITableView委托,然后UITable称为photoTable,单元格称为 photoCell。我试图找出下面的代码来将图像添加到表格中但没有成功。非常感谢任何帮助。

- (IBAction)takePhoto:(id)sender 
{
[self startCameraControllerFromViewController: self usingDelegate: self];
}

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image;
image = (UIImage *)
[info valueForKey:UIImagePickerControllerOriginalImage];
photoImageView.image=image;
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
[picker dismissViewControllerAnimated:YES completion:nil];
}

-(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}

- (BOOL) startCameraControllerFromViewController: (UIViewController*) controller usingDelegate: (id <UIImagePickerControllerDelegate, UINavigationControllerDelegate>) delegate
{
if (([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera] == NO) || (delegate == nil) || (controller == nil)) return NO;
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraUI.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
cameraUI.allowsEditing = NO;
cameraUI.delegate = delegate;
[controller presentViewController:cameraUI animated:YES completion:nil];
return YES;
}

- (UITableViewCell*)tableView:(UITableView*)photoTable cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString* CellIdentifier = @"photoCell";

UITableViewCell* cell = [photoTable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell.imageView.image = [UIImage imageNamed:@"nameOfImage.jpg"];

return cell;
}
4

4 回答 4

2

在文件中创建图像propertytableview出口.h

@property(nonatomic, strong) UIIImage *photoImage;
@property(nonatomic, strong) IBOutlet UITableView *tableview;

在 .m 文件中的didFinishPickingMediaWithInfo:方法中

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
    UIImage *image;
    image = (UIImage *)
    [info valueForKey:UIImagePickerControllerOriginalImage];
    self.photoImage = image;
    [self.tableView reloadData]; //call reloadData method to load data with image
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    [picker dismissViewControllerAnimated:YES completion:nil];
    }

在 cellForRowAtIndexPath: 方法中

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

        UITableViewCell *cell = [self.movieTable dequeueReusableCellWithIdentifier:@"cell"];
        cell.imageView.image = self.photoImage; 

       or use array and add image to array in didFinishPickingMediaWithInfo: and use the image

       cell.imageView.image = [self.photoArray objectAtIndex:0]; 

}
于 2013-11-08T06:56:31.813 回答
2

在您的以下方法中:

imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

添加这一行:

[_tableView reloadData];

返回更新的行数:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

并在cellForRowAtIndexPath中修改您的代码,如下所示:

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.imageView.image = [UIImage imageNamed:@"nameOfImage.jpg"];

注意:如果您想显示多个图像,例如您捕获的每张图像,则将图像保存在数组中。并在cellForRowAtIndexPath中检索它们,并为每个单元格分配不同的图像。

于 2013-11-08T06:59:56.160 回答
1
if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

        cell.imageView.image = [UIImage imageNamed:@"nameOfImage.jpg"];
    }

试试看,希望对你有帮助!

于 2013-11-08T06:51:27.107 回答
1

我认为您必须在拍摄照片后将图像保存在文档目录中

像:

- (void)saveImage:(UIImage*)imagepk
{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
    UIImage *image = imagepk; // imageView is my image from camera
    NSData *imageData = UIImagePNGRepresentation(image);
    [imageData writeToFile:savedImagePath atomically:NO];
    // Code for saving image here

}

然后从文档目录访问并添加到数组中。

于 2013-11-08T07:12:16.980 回答