我的应用在UIButton
上有一个takePhoto UIViewcontroller
,可让用户使用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;
}
答案 0 :(得分:2)
在property
档案中创建图片tableview
和.h
商店
@property(nonatomic, strong) UIIImage *photoImage;
@property(nonatomic, strong) IBOutlet UITableView *tableview;
在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];
}
in cellForRowAtIndexPath:method
- (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];
}
答案 1 :(得分: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 中检索它们,并为每个单元格分配不同的图像。
答案 2 :(得分:1)
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.imageView.image = [UIImage imageNamed:@"nameOfImage.jpg"];
}
尝试一下,我希望它有所帮助!
答案 3 :(得分: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
}
然后从文档目录访问并添加数组。