Ios 拍摄照片并添加到UITableView
我有一个应用程序,它在Ios 拍摄照片并添加到UITableView,ios,objective-c,uitableview,uiimagepickercontroller,Ios,Objective C,Uitableview,Uiimagepickercontroller,我有一个应用程序,它在UIViewcontroller上有一个takePhotoui按钮,用户可以使用imagePickerController拍照。然后,我希望将照片放置在UITableView中 照片部分工作正常,出于测试目的,我将图像显示在UIImagephotoImageView.image中 我添加了UITableView委托,然后UITable被称为photoTable,该单元被称为photoCell。我试图找出下面将图像添加到表中的代码,但没有成功。非常感谢您的帮助 - (IBAc
UIViewcontroller
上有一个takePhotoui按钮,用户可以使用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;
}
-(iAction)拍照:(id)发件人
{
[self startCameraControllerFromViewController:self-usingDelegate:self];
}
-(void)imagePickerController:(UIImagePickerController*)picker未使用信息完成PickingMediaWithInfo:(NSDictionary*)信息
{
UIImage*图像;
image=(UIImage*)
[信息值forkey:UIImagePickerController原始图像];
photoImageView.image=图像;
UIImageWriteToSavedPhotosAlbum(图像,无,无,无);
[picker DismissionViewControllerInitiated:是完成:无];
}
-(无效)ImagePickerController IDCancel:(UIImagePickerController*)选择器
{
[picker DismissionViewControllerInitiated:是完成:无];
}
-(BOOL)从ViewController:(UIViewController*)使用委托的控制器启动MeracontrollerFromViewController:(id)委托
{
如果(([UIImagePickerController isSourceTypeAvailable:UIImagePickerController SourceTypeCamera]==NO)| |(delegate==nil)| |(controller==nil))返回NO;
UIImagePickerController*cameraUI=[[UIImagePickerController alloc]init];
cameraUI.sourceType=UIImagePickerControllerSourceTypeCamera;
cameraUI.mediaTypes=[UIImagePickerController可用于源类型的媒体类型:UIImagePickerController源类型Camera];
cameraUI.allowsEditing=否;
cameraUI.delegate=代表;
[controller presentViewController:cameraUI动画:是完成:无];
返回YES;
}
-(UITableViewCell*)tableView:(UITableView*)photoTable cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“光电池”;
UITableViewCell*单元格=[photoTable dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil)
cell.imageView.image=[UIImage ImageName:@“nameOfImage.jpg”];
返回单元;
}
试试看,希望对你有帮助
试试看,希望对你有帮助 在.h
文件中创建图像属性
,以及表格视图
出口
@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];
}
在.h
文件中创建图像属性
,以及表格视图
出口
@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];
}
使用以下方法:
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中检索它们,并为每个单元格分配不同的图像。使用以下方法:
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中检索它们,并为每个单元格分配不同的图像。我认为您必须在捕获照片后将图像保存在文档目录中
比如:
然后从文档目录和外接程序数组中进行访问。我认为您必须在捕获照片后将图像保存到文档目录中
比如:
然后从文档目录和加载项数组进行访问。if(cell==nil){cell=[[uitableViewCellAlloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:CellIdentifier];}if(cell==nil){cell=[[uitableViewCellAlloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:CellIdentifier];}谢谢我应该补充一点,我已经在-(UITableViewCell*)tableView:(UITableView*)photoTable例程中设置了一个调试,但它甚至没有到达。因此,在此之前,甚至不将其命名为.cell.imageView.image=[UIImage ImageName:@“nameOfImage.jpg”];如果您有不同的图像组,请将此语句从括号中删除。我应该补充一点,我已经在-(UITableViewCell*)tableView:(UITableView*)photoTable例程中设置了一个调试,但它甚至没有到达。因此,在此之前,甚至不将其命名为.cell.imageView.image=[UIImage ImageName:@“nameOfImage.jpg”];如果您有不同的图像集,请将此陈述从括号中删除。非常感谢您的帮助。我面临的问题是根本没有调用cellForRowAtIndexPath方法。。。[self.tableView reloadData]似乎不起任何作用。您是否已创建了从故事板到.h文件的出口,请检查uitableview数据源和代理协议。非常感谢您的帮助。我面临的问题是根本没有调用cellForRowAtIndexPath方法。。。[self.tableView reloadData]似乎不起任何作用。您是否已创建了从故事板到.h文件的出口,请检查uitableview数据源和代理协议。非常感谢您的帮助。请参阅上面对Suhit的评论。我认为这与将代理链接到tableview有关,但我似乎无法使其正常工作。您是否已将tableview从.xib文件链接到文件所有者(您的视图控制器)??检查一下。。。然后,在viewDidLoad方法中,添加以下行:_tableView.delegate=self_tableView.dataSource=self;并实施所有必要的委托方法非常感谢您的帮助。请参阅上面对Suhit的评论。我认为这与