2016-04-10 8 views
0

2つの画像ビュー(imageView & imageTwo)と4つのボタン(各画像ビューのtakePhotoとselectPhoto)があります。各ボタンをタップすると、撮影または選択した写真が対応する画像ビューに表示されます。すばらしいです。同じ画像を2つの別々のフィールドに保存していますか?

ただし、私は2枚の写真をデータベースに保存しようとしています。何らかの理由で、撮影した最初の写真が2つの異なるデータベースフィールドに保存されます(同じ写真が2つの別々のフィールドに保存された写真の代わりに両方のフィールドに表示されます)。つまり、データは正しく保存されているようです(XML出力を参照)。

これはなぜですか?以下のコードを参照してください(長い投稿には申し訳ありません)。保存時に

viewcontroller.m

//PHOTO ONE UPLOAD TAKE AND SELECT 

    - (IBAction)selectPhoto:(id)sender { 
     UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
     picker.delegate = self; 
     picker.allowsEditing = YES; 
     picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
     selectedImageView = self.imageView; // Add this 

     [self presentViewController:picker animated:YES completion:NULL]; 
    } 

    - (IBAction)takePhoto:(id)sender { 
     UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
     picker.delegate = self; 
     picker.allowsEditing = YES; 
     picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
     selectedImageView = self.imageView; // Add this 

     [self presentViewController:picker animated:YES completion:NULL]; 
    } 

    - (IBAction)takePhotoTwo:(id)sender { 

     UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
     picker.delegate = self; 
     picker.allowsEditing = YES; 
     picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
     selectedImageView = self.imageTwo; // Add this 

     [self presentViewController:picker animated:YES completion:NULL]; 

    } 

    - (IBAction)selectPhotoTwo:(id)sender { 

     UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
     picker.delegate = self; 
     picker.allowsEditing = YES; 
     picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
     selectedImageView = self.imageTwo; // Add this 

     [self presentViewController:picker animated:YES completion:NULL]; 


    } 

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
     UIImage *image = info[UIImagePickerControllerEditedImage]; 
     selectedImageView.image = image; 

     [picker dismissViewControllerAnimated:YES completion:NULL]; 
    } 


    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { 

     [picker dismissViewControllerAnimated:YES completion:NULL]; 

    } 


- (IBAction)saveButton:(id)sender { 


    //FIRST IMAGE DATA 

    NSData *imgData = UIImageJPEGRepresentation(self.imageView.image, 0.5); 

    NSMutableDictionary *file = [[NSMutableDictionary alloc] init]; 
    NSString *base64Image = [imgData base64EncodedString]; 
    [file setObject:base64Image forKey:@"file"]; 
    NSString *timestamp = [NSString stringWithFormat:@"%d", (int)[[NSDate date] timeIntervalSince1970]]; 
    NSString *imageTitle = _itemName.text; 
    NSString *filePath = [NSString stringWithFormat:@"%@%@.jpg",@"public://stored/", imageTitle]; 
    NSString *fileName = [NSString stringWithFormat:@"%@.jpg", imageTitle]; 
    [file setObject:filePath forKey:@"filepath"]; 
    [file setObject:fileName forKey:@"filename"]; 
    [file setObject:timestamp forKey:@"timestamp"]; 
    NSString *fileSize = [NSString stringWithFormat:@"%lu", (unsigned long)[imgData length]]; 
    [file setObject:fileSize forKey:@"filesize"]; 


    //SECOND IMAGE DATA 

    NSData *secondimgData = UIImageJPEGRepresentation(self.imageTwo.image, 0.5); 

    NSMutableDictionary *secondfile = [[NSMutableDictionary alloc] init]; 
    NSString *secondbase64Image = [secondimgData base64EncodedString]; 
    [file setObject:secondbase64Image forKey:@"file"]; 
    NSString *secondtimestamp = [NSString stringWithFormat:@"%d", (int)[[NSDate date] timeIntervalSince1970]]; 
    NSString *secondimageTitle = secondtimestamp; 
    NSString *secondfilePath = [NSString stringWithFormat:@"%@%@.jpg",@"public://stored/", secondimageTitle]; 
    NSString *secondfileName = [NSString stringWithFormat:@"%@.jpg", secondimageTitle]; 
    [secondfile setObject:secondfilePath forKey:@"filepath"]; 
    [secondfile setObject:secondfileName forKey:@"filename"]; 
    [secondfile setObject:secondtimestamp forKey:@"timestamp"]; 
    NSString *secondfileSize = [NSString stringWithFormat:@"%lu", (unsigned long)[secondimgData length]]; 
    [secondfile setObject:secondfileSize forKey:@"filesize"]; 



    [DIOSFile fileSave:file success:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSLog(@"File uploaded!"); 

     //FIRST IMAGE FILE 

     [file setObject:[responseObject objectForKey:@"fid"] forKey:@"fid"]; 
     [file removeObjectForKey:@"file"]; 

     fid = [responseObject objectForKey:@"fid"]; 

     NSLog(@"%@",responseObject); 


     NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 

     [dict setObject: [NSString stringWithFormat:@"%@", fid] forKey:@"fid"]; 
     NSLog(@"%@", fid); 

     NSDictionary *fidLangDict = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:dict] forKey:@"und"]; 

     [nodeData setObject:fidLangDict forKey:@"field_photo"]; 



    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Node did not save!"); 
    }]; 



    [DIOSFile fileSave:file success:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSLog(@"File uploaded!"); 

     //SECOND IMAGE FILE 


     [secondfile setObject:[responseObject objectForKey:@"fid"] forKey:@"fid"]; 
     [secondfile removeObjectForKey:@"file"]; 

     fid = [responseObject objectForKey:@"fid"]; 

     NSLog(@"%@",responseObject); 


     NSMutableDictionary *secondDict = [NSMutableDictionary dictionary]; 

     [secondDict setObject: [NSString stringWithFormat:@"%@", fid] forKey:@"fid"]; 
     NSLog(@"%@", fid); 

     NSDictionary *secondfidLangDict = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:secondDict] forKey:@"und"]; 

     [nodeData setObject:secondfidLangDict forKey:@"phototwo"]; 


     [DIOSNode nodeSave:nodeData success:^(AFHTTPRequestOperation *operation, id responseObject) { 
      NSLog(@"Node saved!"); 
      UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil]; 
      ipadaccountViewController *AccountViewController = (ipadaccountViewController *)[storyboard instantiateViewControllerWithIdentifier:@"MyAccount"]; 
      [self.navigationController popViewControllerAnimated:TRUE]; 



     } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
      NSLog(@"Node did not save!"); 
     }]; 



    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Node did not save!"); 
    }]; 

XMLデータ:

<field_photo> 
<und is_array="true"> 
<item> 
<fid>265</fid> 
<uid>5</uid> 
<filename>phone.jpg</filename> 
<uri>public://stored/phone_1.jpg</uri> 
<filemime>image/jpeg</filemime> 
<filesize>161858</filesize> 
<status>1</status> 
<timestamp>1460264800</timestamp> 
<rdf_mapping/> 
<alt/> 
<title/> 
<width>1536</width> 
<height>1536</height> 
</item> 
</und> 
</field_photo> 

<phototwo> 
<und is_array="true"> 
<item> 
<fid>266</fid> 
<uid>5</uid> 
<filename>phone.jpg</filename> 
<uri>public://stored/phone_2.jpg</uri> 
<filemime>image/jpeg</filemime> 
<filesize>161858</filesize> 
<status>1</status> 
<timestamp>1460264800</timestamp> 
<rdf_mapping/> 
<alt/> 
<title/> 
<width>1536</width> 
<height>1536</height> 
</item> 
</und> 
</phototwo> 

答えて

0

私は2番目の[DIOSFile fileSave...で、それはsecondfileの代わりfileされるべきだと思います。

関連する問題