【问题标题】:Take a photo and add to UITableView拍照并添加到 UITableView
【发布时间】:2013-11-08 06:42:28
【问题描述】:

我有一个应用程序,在 UIViewcontroller 上有一个 takePhoto UIButton,使用户可以使用 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;
}

【问题讨论】:

  • if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; }

标签: ios objective-c uitableview uiimagepickercontroller


【解决方案1】:

在.h 文件中创建图像property 和tableview 出口

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

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

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

}

【讨论】:

  • 非常感谢您的帮助。我面临的问题是根本没有调用 cellForRowAtIndexPath 方法...... [self.tableView reloadData] 似乎没有做任何事情。
  • 您是否创建了从情节提要到 .h 文件的出口,还要检查 uitableview 数据源和委托协议。
【解决方案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 中检索它们,并为每个单元格分配不同的图像。

【讨论】:

  • 非常感谢您的帮助。请参阅上面对 Suhit 的评论。而且我认为这与将委托链接到 tableview 有关,但我似乎无法让它工作。
  • 您是否已将您的 tableview 从 .xib 文件链接到文件所有者(您查看控制器)?检查...之后,在您的 viewDidLoad 方法中,添加以下行: _tableView.delegate = self; _tableView.dataSource = 自我;并实现所有必需的委托方法
【解决方案3】:
if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

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

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

【讨论】:

  • 谢谢。我应该补充一点,我已经在 - (UITableViewCell*)tableView:(UITableView*)photoTable 例程中设置了一个调试,它甚至没有到达它。所以在此之前出现了问题,甚至不能调用它。
  • cell.imageView.image = [UIImage imageNamed:@"nameOfImage.jpg"];如果您有不同的图像集,请将此声明放在括号中
【解决方案4】:

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

喜欢:

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

}

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

【讨论】:

  • 谢谢 - 这将是我接下来要看的东西。很有帮助。顺便说一句,由于我必须在 iPad 上执行此操作(照片无法在模拟器上运行),您知道如何检查文档目录以查看照片是否保存在那里吗?
  • NSArray paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString path = [documentsDirectory stringByAppendingPathComponent:imageName]; UIImage* image = [UIImage imageWithContentsOfFile:path];
猜你喜欢
  • 2014-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多