gpt4 book ai didi

iphone - JSON 图像解析/前缀 URL

转载 作者:可可西里 更新时间:2023-11-01 05:55:22 26 4
gpt4 key购买 nike

我已经使用 JSON/PHP/MYSQL 成功解析了表格 View 中的文本和图像。我只将图像的位置存储在数据库中,实际图像存储在我服务器上的目录中。数据库中唯一存储的与图像相关的是名称。示例 car.jpg。我想要做的是在我的服务器上为图像位置的 URL 添加前缀,这样它们就可以被解析,而无需进入数据库并手动输入 URL。这是我的一些代码...

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *identifier = @"studentsCell";

StudentsCell *cell = (StudentsCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StudentsCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSDictionary *studentsDict = [students objectAtIndex:indexPath.row];
//I want to prefix the URL for the key imagepath but i dont know where and how to do it.
NSURL *imageURL = [NSURL URLWithString:[studentsDict objectForKey:@"imagepath"]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *imageLoad = [[UIImage alloc] initWithData:imageData];

cell.imageView.image = imageLoad;

NSString *name = [NSString stringWithFormat:@"%@ %@", [studentsDict valueForKey:@"first"], [studentsDict valueForKey:@"last"]];

cell.title.text = name;

NSString *subtitle = [NSString stringWithFormat:@"%@", [studentsDict objectForKey:@"email"]];

cell.subtitle.text = subtitle;

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(265, 6, 44, 44);
[button setImage:[UIImage imageNamed:@"email.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(email:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];

// cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellbackground.png"]];

return cell;
}

最佳答案

假设您有类似的东西:

NSURL *baseURL = [NSURL URLWithString:@"http://www.your.site.here/images"]; // whatever the folder with the images is

然后你可以这样做:

NSURL *imageURL = [baseURL URLByAppendingPathComponent:[studentsDict objectForKey:@"imagepath"]];

顺便说一句,你应该考虑使用UIImageView 类别,例如SDWebImage .然后,您可以执行异步图像加载,而不是将图像数据同步加载到 NSData 中:

[cell.imageView setImageWithURL:imageURL
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

占位符是在加载图像时应该显示的内容(可能只是一个空白图像),然后 SDWebImage 将异步检索图像并在检索到时更新单元格。这将产生一个更加灵敏的用户界面。它还将利用图像缓存(因此,如果您向下滚动然后向上滚动,将不会再次检索图像)。

AFNetworking有一个类似的 UIImageView 类别,但不是很健壮的实现。但如果您已经在使用 AFNetworking,这是一个选择。

关于iphone - JSON 图像解析/前缀 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18197513/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com