我有一个 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