如何从JSON String转换图像数据?

时间:2014-06-10 15:53:20

标签: xcode uiimage nsdata

我正在尝试将图像加载到tableview单元格中。 代码没问题,除了它不喜欢来自NSString的图像 - 这是可以理解的!

我如何a)从mySQL数据库加载图像 - 我是否将它们保存在数据库中作为网站Image文件夹的链接,即路径? b)我将数据作为JSON字符串加载到NSString。 如何将图像部分转换为UIImage? 我的代码如下 - 当然,除了图像部分外,一切运行正常。 我是否使用NSData进行转换? 提前谢谢了。 @implementation ViewController

- (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 个答案:

答案 0 :(得分:0)

A)取决于图像的大小,我认为。较大的文件最好存储在文件系统中,并在db中有链接。对于较小的图像,您可以直接将它们存储在数据库中。这是一个很好的参考: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];