在Sqlite数据库中存储错误的图像文件路径。 Objective C,iOS

时间:2013-06-06 16:38:04

标签: ios objective-c uipickerview nsdocumentdirectory

我有一个sqlite数据库,我可以在其中存储图像和数据,然后将其显示在ViewController进行编辑。

我在将数据库存储到路径时没有问题。

我遇到一些麻烦,当我使用相机或照片卷时,我使用以下代码UIImagePickerController。 UIPickerController类将图像保存到目录,保存类保存图像路径,但重命名它。

但是最大的麻烦在于我使用类似的代码发送到我的文件所在的位置 我将我的东西保存在我的数据库中。

这段代码的作用是什么,因为它只在我推送保存后执行,它是从我第一次结束UIImagePickerController时重命名文件路径这意味着我将错误的图像路径存储到数据库中它显示在上一篇文章。

所以在我使用相机或照片后,我在NSLOG中获得以下内容:

当我使用UIImagePickerController时(这是我想要放入数据库的路径)

2013-06-06 17:50:48.019 shotplacementguide001[7235:907] photo_1.png - - /var/mobile/Applications/EE759F67-DA8F-4232-BE4F-35D16D047D24/Documents/photo_1.png

这是因为当我推送保存时:

2013-06-06 17:50:53.827 shotplacementguide001[7235:907] photo_2.png - - /var/mobile/Applications/EE759F67-DA8F-4232-BE4F-35D16D047D24/Documents/photo_2.png

这是我UIImagePickerController的代码:

 -(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSError *error = nil;
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];
NSString *fileName = [NSString stringWithFormat:@"photo_%i.png", [dirContents count]];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:fileName];
// self.presentImageFileName = [NSString stringWithFormat:fileName];
self.presentImageFileName = [NSString stringWithFormat:@"%@", fileName];
NSLog(@"%@ - - %@", self.presentImageFileName,imagePath );


// Then save the image to the Documents directory
NSString *mediaType1 = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType1 isEqualToString:@"public.image"]){
    UIImage *editedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    NSData *webData = UIImagePNGRepresentation(editedImage);
    [webData writeToFile:imagePath atomically:YES];
}

NSString *mediaType = info[UIImagePickerControllerMediaType];

[self dismissViewControllerAnimated:YES completion:nil];

if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
    UIImage *image = info[UIImagePickerControllerOriginalImage];

    animalEditImageView.image = image;
    if (_newMedia)
        UIImageWriteToSavedPhotosAlbum(image,
                                       self,
                                       @selector(image:finishedSavingWithError:contextInfo:),
                                       nil);
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
    // Code here to support video if enabled
}
}

当我推送保存时:

-(IBAction) done:(id) sender
{
Customer *customer = [[Customer alloc] init];
    customer.dateTimeZone = self.dateTimeTextField.text;
    customer.species = self.speciesTextField.text;
    customer.gender = self.genderTextField.text;
    customer.location_name = self.locationTextField.text;
    customer.latitude = self.speciesTextField.text;
    customer.longitude = self.speciesTextField.text;
    customer.firearm = self.firearmTextField.text;
    customer.comments = self.commentsTextField.text;

    // Capture the file name of the image
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fileName = addCustomerFileName;
    self.customerToEdit.imagePath = [documentsDirectory stringByAppendingPathComponent:fileName];
    // self.presentImageFileName = [NSString stringWithFormat:fileName];
    self.presentImageFileName = [NSString stringWithFormat:@"%@", fileName];
    NSLog(@"%@ - - %@", self.presentImageFileName,self.customerToEdit.imagePath );
}

如何更改上面的代码,只将我在-(void)imagePickerController:(UIImagePickerController *)picker中保存的图像路径重命名,而不是将重命名的图像实际保存到目录中,就像我做错了一样。

因为我保存了错误的图像路径,因此它将显示在上一个条目EditViewController中。

我在我拥有的UIPickerController类中使用customer.imagePath时遇到问题,并且因为这个原因在我的保存类中使用了info:

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

2 个答案:

答案 0 :(得分:1)

我最终编写了一个解决方案,该解决方案只是从UIPhotoPickerController获取文件路径并将其存储而不是重命名。

 // Capture the file name of the image
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fileName = addCustomerFileName;
    self.customerToEdit.imagePath = [documentsDirectory stringByAppendingPathComponent:fileName];
    // self.presentImageFileName = [NSString stringWithFormat:fileName];
    self.presentImageFileName = [NSString stringWithFormat:@"%@", fileName];
    NSLog(@"%@ - - %@", self.presentImageFileName,self.customerToEdit.imagePath );

答案 1 :(得分:0)

您的问题是由“命名”算法引起的,那里没有重命名。首先,您获取文档文件夹中的文件计数,对于第一次调用,您的日志似乎是1,您将图像命名为image_1.jpg并将其保存到documents目录中之后,您只需在2文件夹中包含documents个文件,因此当您再次尝试获取图像文件路径时,文档文件夹中的文件数将为2,因此您可以获得{{1} }} 文件名。

所以我们确定了问题,现在让我们解决它。如果可以的话,你应该做的是保存文件路径,并将其发送到将其添加到数据库的视图控制器,或者你只需​​更改命名算法,就可以使用当前日期([NSDate date])as文件名,你对它们进行排序,并获得最后添加的图像...或其他算法。