我正在为基于Drupal的网站构建应用程序。我目前有我的iOS应用程序从MySQL数据库中提取数据(例如文章标题和描述)。有谁知道我会用什么样的代码在UIImageView中显示文章的相应图像(通过Drupal字段上传)?
以下是我用来从Drupal网站提取文本的代码的快速摘录。
DoctorsViewController.h
@interface DoctorsViewController : UIViewController {
IBOutlet UITableView *DoctorsTableView;
NSArray *Doctors;
NSMutableData *data;
}
DoctorsViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURL *url = [NSURL URLWithString:@"MY URL HERE"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
data = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
[data appendData:theData];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
Doctors = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
[DoctorsTableView reloadData];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The download could not complete - please make sure that you're connected to 3G or Wi-Fi." delegate:nil
cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[errorView show];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (int)numberOfSectionsInTableView: (UITableView *)tableview
{
return 1;
}
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [Doctors count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *DoctorsTableIdentifier = @"DoctorsCell";
DoctorsCell *cell = (DoctorsCell *)[tableView dequeueReusableCellWithIdentifier:DoctorsTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DoctorsCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.firstnameLabel.text = [[Doctors objectAtIndex:indexPath.row] objectForKey:@"node_title"];
cell.descriptionLabel.text = [[Doctors objectAtIndex:indexPath.row] objectForKey:@"Opening Paragraph"];
return cell;
}
答案 0 :(得分:0)
这并不像听起来那么简单 - Drupal以非常特定的方式存储其字段数据,并且图像的URI使用模式而不是具有相对或绝对路径来寻址(例如public:// path / to / image.jpg而不是/sites/default/files/path/to/image.jpg)
您可以直接从DB获取节点的图像URI(假设您的文章类型使用标准的field_image字段):
SELECT fm.uri
FROM {file_managed} fm
INNER JOIN field_data_field_image fi ON fi.entity_type = 'node' AND fi.bundle = 'article' AND fi.deleted = 0 AND fi.field_image_fid = fm.fid
WHERE fi.entity_id = <nid>
其中<nid>
是节点ID。从那里开始,您需要执行将路径转换为适用于iOS应用程序的所需逻辑。 Drupal使用file_create_url()
执行相同的任务,因此这是您获得灵感的最佳起点。根据您的设置,您只需在public://
到http://example.com/sites/default/files/
上执行字符串替换即可。
从长远来看,您可能希望在应用中添加Services module并使用REST API,而不是直接查询数据库。这需要很多准备Drupal数据的压力,所以你可以只处理你需要的东西。