2

我想用相机拍照并保存在文档目录中,名称和图像在 UITableViewCell 中是 veiw。

如果我选择任何一行,则相应的行图像将转到带有名称和图像的下一个视图。

请给出您的想法、建议和参考代码。

4

3 回答 3

2

尝试此方法将 UIImage 存储在本地目录中...

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

 UIImage *pickedImage = [info objectForKey:UIImagePickerControllerEditedImage];
 [self addImage:pickedImage toCacheWithIdentifier:@"NAME OF THE IMAGE"];

}

- (void)addImage:(UIImage *)image toCacheWithIdentifier:(NSString *)identifier {

    NSString *folderPath = @"LOCAL DIRECTORY PATH ";
    if(![[NSFileManager defaultManager] fileExistsAtPath:folderPath isDirectory:nil]) {

        [[NSFileManager defaultManager] createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];
    }

    NSString *fileName = [NSString stringWithFormat:@"%@.png",identifier];

 fileName = [folderPath stringByAppendingPathComponent:fileName];

 NSData *imageData = UIImagePNGRepresentation(image);

   [imageData writeToFile:fileName atomically:YES];
}

要在单击 tableView 单元格时检索图像,请从didselectRowAtIndexPath....调用此方法。

- (UIImage *)imageFromCacheWithIdentifier:(NSString *)identifier {

    NSString *folderPath = @"LOCAL DIRECTORY PATH ";

    NSString *fileName = [NSString stringWithFormat:@"%@.png",identifier];

    fileName = [folderPath stringByAppendingPathComponent:fileName];

    if([UIImage imageWithContentsOfFile:fileName]) {

        return [UIImage imageWithContentsOfFile:fileName];

    }

    return nil;
}
于 2013-10-15T11:10:19.227 回答
1

UIImagePickerControllerDelegate在您的.h文件中实现。

实现它的方法

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *anImage = [info valueForKey:UIImagePickerControllerOriginalImage];

    NSData *imageData = UIImageJPEGRepresentation(anImage, 1.0);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *path = [paths objectAtIndex:0];

    NSString *tmpPathToFile = [NSString stringWithFormat:@"%@/specificImagedName.jpg", path];

    if([imageData writeToFile:tmpPathToFile atomically:YES])
    {
          //Write was successful. 
    }
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    // Dismiss your UIImagePickerController
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

UITableViewDelegate,UITableViewDataSource在你的.h文件中实现 然后实现

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

    UITableViewCell *cell =[ tableView dequeueReusableCellWithIdentifier:@"cell"];
    UIImage * image;
    UILabel * Title;

    if(cell==nil)
    {
        cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]autorelease];

        Title     = [[UILabel alloc]initWithFrame:CGRectMake(70, 0, cell.contentView.frame.size.width - 14, cell.contentView.frame.size.height)];
        [Title setTag:111];
        [cell.contentView addSubview:invoiceTitle];
        [Title release];

        image     = [[UIImage alloc]initWithFrame:CGRectMake(14, 0, 50, cell.contentView.frame.size.height)];
        [image setTag:222];
        [cell.contentView addSubview:projectTitle];
        [image release];

    }
    else
    {

        Title = (UILabel *)[cell.contentView viewWithTag:111];
        image = (UIImage *)[cell.contentView viewWithTag:222];
    }


    [image setText:[UIImage imageWithContentsOfFile:YOUR_IMAGE_PATH]];

    [Title setText:YOUR_CONTACT_NAME];

    return cell;
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return NO_OF_SECTIONS;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    /* switch (section) // For different number of rows per section
    {
        case 0:
        {
            return NO_OF_ROWS_IN_SECTION;
            break;
        }

        case 1:
        {
            return NO_OF_ROWS_IN_SECTION;
            break;
        }

        default:
            break;
    }*/

    return NO_OF_ROWS;
}
于 2013-10-15T11:23:18.493 回答
1

使用相机拍照:

   imagePicker = [[UIImagePickerController alloc]init];
    imagePicker.delegate = self;

    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.allowsEditing = YES;
        [self presentViewController:imagePicker animated:YES completion:nil];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message: @"Camera is not available" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];

    }

也添加这个方法。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

    UIImage *pickedImage = [info objectForKey:UIImagePickerControllerEditedImage];
    NSData *data=UIImagePNGRepresentation(pickedImage);

    NSString *FileName=[NSString stringWithFormat:@"test1.png"];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *tempPath = [documentsDirectory stringByAppendingPathComponent:FileName];
    [data writeToFile:tempPath atomically:YES];
    [picker dismissViewControllerAnimated:YES completion:nil];
}

将目录图像显示到 UITablevieCell 只需调用此方法:

 NSString *strPath= [NSString stringWithFormat:@"test.png"];
 NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]; 
 NSString *path = [documentsDirectory stringByAppendingPathComponent:strPath]; 
 NSURL *targetURL = [NSURL fileURLWithPath:path]; 
 NSData *returnData=[NSData dataWithContentsOfURL:targetURL]; 
 UIImage *imagemain=[UIImage returnData];
于 2013-10-15T10:40:57.537 回答