【发布时间】: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