如何从相机拍照并将其名称添加到数据库中?

时间:2013-10-26 10:35:32

标签: iphone ios5

我想从相机中获取照片并使用其名称来保存数据库...

我使用过这些代码。我已成功拍摄照片并添加到图像视图中,但现在我无法获取其名称(字符串值)

ImagePicker = [[UIImagePickerController alloc] init];
            ImagePicker.delegate = self;
            ImagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentModalViewController:ImagePicker animated:YES];

- (void)imagePickerController:(UIImagePickerController *)picker
        didFinishPickingImage:(UIImage *)image
                  editingInfo:(NSDictionary *)editingInfo
{
    [ImagePicker dismissModalViewControllerAnimated:YES];
    imageview.hidden=NO;
    imageview.image = image;

    NSDate *today = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
    NSString *dateString = [dateFormat stringFromDate:today];
    // save image in document directoties
    UIImage *image1=imageview.image;


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
    NSData *pngData = UIImageJPEGRepresentation(image1,100.0);
    NSString *filePath;
    filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"image%@.jpg",dateString]]; //Add the file name
    [pngData writeToFile:filePath atomically:YES]; //Write the file
    NSLog(@"File Path is %@",filePath);

    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath])  //Optionally check if folder already hasn't existed.
    {
        NSLog(@"Unable to create Folder in Documents Directory");

    }

}

2 个答案:

答案 0 :(得分:-1)

我想知道确切的图像名称不是问题,而是为拾取的图像获取唯一的名称将解决您的目的,以便您可以保存图像并通过其名称跟踪它。可能这可以帮到你

//Fetch the Maximum id from DB
NSString* strSelect= [NSString stringWithFormat:@"Select MAX(id) from TableName"]; 
  NSMutableArray*  arr_test  = [[database executeQuery:strSelect]mutableCopy];

   //Check if it is the first Image or not
   if ([[[arr_test objectAtIndex:0]valueForKey:@"MAX(id)"]isKindOfClass:[NSNull class]])
        img_id = 1;

    else{
        NSString *strtest = [[arr_test objectAtIndex:0] valueForKey:@"MAX(id)"];
        img_id=[strtest intValue]+1;
    }

//Give the Image Name with Unique ID
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/image%d.jpg",img_id]]; 

答案 1 :(得分:-1)

- (void)imagePickerController:(UIImagePickerController *)pickerr didFinishPickingMediaWithInfo:(NSDictionary *)info {     [pickerr dismissModalViewControllerAnimated:YES];     //由于我们保持allowEditing = YES,我们使用UIImagePickerControllerEditedImage,否则使用UIImagePickerControllerOriginalImage     UIImage * image = [info objectForKey:@“UIImagePickerControllerEditedImage”];

[self saveImage:image];

}

  • (无效)saveImage:(的UIImage *)imagepk

{

Database *dbObj = [Database Connetion];

//NSLog(@"image width is %f",imagepk.size.width);



NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.jpg",appDelegate.activeId]];

[dbObj updateImage:[NSString stringWithFormat:@"%d.jpg",appDelegate.activeId] :appDelegate.activeId];


UIImage *image = imagepk; // imageView is my image from camera
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];

}