我有一些项目,我必须从JSON粘贴图像。我试着找到关于这个的任何教程,试着看看这个主题的任何视频,但没有。所以我的问题有:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webdata options:0 error:nil];
NSDictionary *playlist =[allDataDictionary objectForKey:@"playlist_data"];
for (NSDictionary *diction in playlist) {
NSString *name = [diction objectForKey:@"text1"];
NSString *namesong = [diction objectForKey:@"text2"];
NSString *images = [diction objectForKey:@"image"];
NSLog(@"%@", images);
[array addObject:text1];
[array2 addObject:text2];
}
[[self tableTrack]reloadData];
}
我添加了文本1到单元格也文本2它的工作完美,但如何将图像添加到数组3(我的tableView中的单元格中的图像)? 我也尝试添加图像,但它不适合我:
NSURL *imageURL = [NSURL URLWithString:[appsdict objectForKey:@"image"]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *imageLoad = [[UIImage alloc] initWithData:imageData];
cell.imageView.image = imageLoad;
请帮助解决我的问题,或者提供一些教程,其中包含来自JSON的解析图像,也是youtube避免了JSON解析的完美教程。谢谢!
答案 0 :(得分:1)
不要保留多个数据源,例如array1
,array2
,array3
等。这不是很好的编码,在调试/解决问题时会混淆。而是维护单个数组,其中包含用于在表视图的单个单元格中显示的信息。
for (NSDictionary *dict in playlist) {
// array is single data source
[array addObject:diction];
}
然后在将数据分配给表视图单元格时使用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] autorelease];
}
cell.text1 = [[array objectAtIndex:indexPath.row] objectForKey:@"text1"];
cell.text2 = [[array objectAtIndex:indexPath.row] objectForKey:@"text2"];
//For displaying image by lazy loading technique use SDWebImage.
[cell.imageView setImageWithURL:[NSURL URLWithString:[[array objectAtIndex:indexPath.row] objectForKey:@"image"]] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
// If no place holder image, use this way
// [cell.imageView setImageWithURL:[NSURL URLWithString:[[array objectAtIndex:indexPath.row] objectForKey:@"image"]] placeholderImage:nil];
}
就像我在评论中提到的从JSON响应中的URL延迟加载图像一样,使用SDWebImage
。用法很简单,如上所示。或者,您可以通过研究Apple的示例代码来自己实现延迟加载:LazyTableImages
希望有所帮助!
答案 1 :(得分:0)
根据https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html和http://www.json.org/,您无法将二进制数据保存在JSON中(除非它位于base64中)。
您的webdata
似乎存在问题,但这不是有效的JSON。在调试时,您是否检查了该字典中@"image"
项下的设置?您也可以使用[NSJSONSerialization isValidJSONObject:webdata]
查看您的数据是否正常。
答案 2 :(得分:0)
//prepare your method here..
-(void)hitimageurl{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:replacephotostring]];
//set your image on main thread.
dispatch_async(dispatch_get_main_queue(), ^{
if (_photosArray==nil) {
_showImageView.image = [UIImage imageNamed:@"noimg.png"];
} else {
[_showImageView setImage:[UIImage imageWithData:data]];
}
});
});