0

画像とデータを保存し、ViewController編集するために表示できるsqliteデータベースがあります。

データベースにパスを保存することに問題はありません。

カメラまたはフォトロールの使用を終了するときに、次のコードを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ます。

このため、持っているクラスでcustomer.imagePath をUIPickerController使用し、保存クラスで info を使用するのに問題があります。

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

2 に答える 2

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 );
于 2013-06-10T16:10:20.383 に答える