如何从 JSON 字符串转换图像数据?
Posted
技术标签:
【中文标题】如何从 JSON 字符串转换图像数据?【英文标题】:How do I convert image data from JSON String? 【发布时间】:2014-06-10 15:53:19 【问题描述】:我正在尝试将图像加载到 tableview 单元格中。 代码没问题,只是它不喜欢来自 NSString 的图像 - 这是可以理解的!
如何 a) 从 mysql 数据库加载图像 - 我是否将它们作为指向网站图像文件夹的链接(即路径)保存在数据库中? b) 我将数据作为 JSON 字符串加载到 NSString。 如何将图像部分转换为 UIImage? 我的代码在下面——当然,除了图像部分之外,一切运行正常。 我是否使用 NSData 进行转换? 提前谢谢了。 @实现视图控制器
- (void)viewDidLoad
[super viewDidLoad];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CarsRUs.png"]];
[self.listTableView setBackgroundView:imageView]; // Set this view controller object as the delegate and data source for the table view
self.listTableView.delegate = self;
self.listTableView.dataSource = self;
// Create array object and assign it to _feedItems variable
_feedItems = [[NSArray alloc] init];
// Create new HomeModel object and assign it to _homeModel variable
_homeModel = [[HomeModel alloc] init];
// Set this view controller object as the delegate for the home model object
_homeModel.delegate = self;
// Call the download items method of the home model object
[_homeModel downloadItems];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
-(void)itemsDownloaded:(NSArray *)items
// This delegate method will get called when the items are finished downloading
// Set the downloaded items to the array
_feedItems = items;
// Reload the table view
[self.listTableView reloadData];
#pragma mark Table View Delegate Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
// Return the number of feed items (initially 0)
return _feedItems.count;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
// Retrieve cell
NSString *cellIdentifier = @"BasicCell";
UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// Get the location to be shown
Location *item = _feedItems[indexPath.row];
// Get references to labels of cell
myCell.textLabel.text = item.address;
myCell.detailTextLabel.text = item.name;
myCell.imageView.image = item.image;
return myCell;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
// Set selected location to var
_selectedLocation = _feedItems[indexPath.row];
// Manually call segue to detail view controller
[self performSegueWithIdentifier:@"detailSegue" sender:self];
#pragma mark Segue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
// Get reference to the destination view controller
DetailViewController *detailVC = segue.destinationViewController;
// Set the property to the selected location so when the view for
// detail view controller loads, it can access that property to get the feeditem obj
detailVC.selectedLocation = _selectedLocation;
@end
【问题讨论】:
【参考方案1】:A) 我认为取决于图像的大小。较大的文件最好存储在文件系统中,链接在数据库中。对于较小的图像,您可以直接将它们存储在数据库中。这是一个很好的参考:http://www.sourcecodester.com/article/other/should-i-save-images-database-or-file-system.html
B)NSData 是要走的路:
NSData *imageData = [NSData dataWithUTF8String:myJsonString];
UIImage *myImage = [UIImage imageWithData:imageData];
【讨论】:
以上是关于如何从 JSON 字符串转换图像数据?的主要内容,如果未能解决你的问题,请参考以下文章