【问题标题】:Storing the wrong image filepath in Sqlite database. Objective C, iOS在 Sqlite 数据库中存储错误的图像文件路径。目标 C,iOS
【发布时间】:2013-06-02 18:47:57
【问题描述】:

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

在我的数据库中存储路径没有问题。

我在使用完相机或照片胶卷后遇到了一些麻烦,我将以下代码与UIImagePickerController 结合使用。 UIPickerController类保存图片到目录,save类保存图片路径但重命名。

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

此代码的作用是因为它仅在我推送 save 后执行,它是否重命名了我第一次结束 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

【问题讨论】:

    标签: ios objective-c uipickerview nsdocumentdirectory


    【解决方案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 );
    

    【讨论】:

      【解决方案2】:

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

      所以我们发现了问题,现在让我们解决它。如果可以的话,您应该保存文件路径,并将其发送到将其添加到数据库的视图控制器,或者您可以简单地更改命名算法,您可以使用当前日期([NSDate date])作为文件名,然后对它们进行排序并获取添加的最后一张图片...或其他算法。

      【讨论】:

      • 我已经可以做到了,我的问题本质上是如何在我的保存类中更改此语句以反映我已经在我的选择器中获得的路径? NSString *fileName = [NSString stringWithFormat:@"photo_%i.png", [dirContents count]];
      • 但不是你提到的 NSdate。
      • 这就像我用上面评论中的声明做了一个假路径。因为 photo_2.png 实际上并不存在于目录中。
      猜你喜欢
      • 1970-01-01
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-02
      • 1970-01-01
      • 2014-08-05
      • 1970-01-01
      相关资源
      最近更新 更多