I'm building an app using the Drupal iOS SDK, and at the moment, I'm trying to make it so that my user can take a photo of something using the camera, save that image, upload it, and attach it to the newly created node. The image saves and uploads to my server just fine, and a node is created with my title and body fields, however I can't seem to get the image to attach to the created node (field_photo). Note: The field type is "image". See code below:
CameraViewController.m
- (IBAction)saveButton:(id)sender {
NSMutableDictionary *nodeData = [NSMutableDictionary new];
[nodeData setValue:_itemName.text forKey:@"title"];
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"];
[DIOSFile fileSave:file success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"File Saved!");
[file setObject:[responseObject objectForKey:@"fid"] forKey:@"fid"];
[file removeObjectForKey:@"file"];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failed to save file!");
}];
NSDictionary *bodyValues = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:_itemDescrip.text, nil] forKeys:[NSArray arrayWithObjects:@"value", nil]];
NSDictionary *languageDict = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:bodyValues] forKey:@"und"];
[nodeData setObject:languageDict forKey:@"body"];
NSDictionary *fidLangDict = [NSDictionary dictionaryWithObject:@[file] forKey:@"und"];
[nodeData setObject:fidLangDict forKey:@"field_photo"];
NSLog(@"%@",fidLangDict);
[nodeData setObject:@"storage_item" forKey:@"type"];
[nodeData setObject:@"und" forKey:@"language"];
[DIOSNode nodeSave:nodeData success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Item successfully added to Database!");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failed to add item to database!");
}];
Aucun commentaire:
Enregistrer un commentaire