【问题标题】:Same image saving to two separate fields?相同的图像保存到两个单独的字段?
【发布时间】:2016-04-10 05:25:06
【问题描述】:

我有 2 个图像视图(imageView 和 imageTwo)和四个按钮(每个图像视图的 takePhoto 和 selectPhoto)。点击每个按钮会在相应的图像视图中显示拍摄或选择的照片。伟大的。

但是,我正在尝试将拍摄的两张照片保存到我的数据库中。出于某种原因,拍摄的第一张照片保存到我的两个不同的数据库字段中(同一张照片出现在两个字段中,而不是每张不同的照片保存到两个单独的字段中)。也就是说,数据似乎正确保存(请参阅 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>

【问题讨论】:

    标签: ios objective-c image imageview


    【解决方案1】:

    我认为在第二个[DIOSFile fileSave... 中应该是secondfile 而不是file

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-02
      • 2021-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多