如何显示来自 JSON 的图像的水平滚动视图
Posted
技术标签:
【中文标题】如何显示来自 JSON 的图像的水平滚动视图【英文标题】:how to display horizontal scrollview of images from the JSON 【发布时间】:2017-01-24 10:22:35 【问题描述】:我的 JSON 响应
"result":0,"data":["bannerslider_id":"1","img":"http:\/\/whatsinthestreet.com\/media\/bannerslider\/images\/slider1.jpg","bannerslider_id":"2","img":"http:\/\/whatsinthestreet.com\/media\/bannerslider\/images\/slider2.jpg","bannerslider_id":"3","img":"http:\/\/whatsinthestreet.com\/media\/bannerslider\/images\/slider3.jpg","bannerslider_id":"4","img":"http:\/\/whatsinthestreet.com\/media\/bannerslider\/images\/slider4.jpg"]
我写了这样的代码
(void)viewDidAppear:(BOOL)animated
[[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:@"http://whatsinthestreet.com/allbanners.php"] completionHandler:^(NSData * _Nullable data ,NSURLResponse * _Nullable response,NSError * _Nullable error)
NSLog(@"response %@",response);
NSLog(@"data %@",data);
NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"dict%@",dict);
NSArray *array;
array=[dict valueForKey:@"data"];
NSLog(@"%@",array);
我得到的回应是
dict
data = (
"bannerslider_id" = 1;
img = "http://whatsinthestreet.com/media/bannerslider/images/slider1.jpg";
,
"bannerslider_id" = 2;
img = "http://whatsinthestreet.com/media/bannerslider/images/slider2.jpg";
,
"bannerslider_id" = 3;
img = "http://whatsinthestreet.com/media/bannerslider/images/slider3.jpg";
,
"bannerslider_id" = 4;
img = "http://whatsinthestreet.com/media/bannerslider/images/slider4.jpg";
);
result = 0;
如何从这些值访问图像以获取图像的水平滚动视图?
【问题讨论】:
[array valueforkey:@"img"] objectAtIndex:0] 你得到了第一个横幅图片 您的滚动视图代码在哪里?你在哪里创建图像? 【参考方案1】:这应该适合你。
-(void)viewDidAppear:(BOOL)animated
[[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:@"http://whatsinthestreet.com/allbanners.php"] completionHandler:^(NSData * _Nullable data ,NSURLResponse * _Nullable response,NSError * _Nullable error)
NSLog(@"response %@",response);
NSLog(@"data %@",data);
NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"dict%@",dict);
NSArray *array;
array=[dict objectForKey:@"data"];
NSLog(@"%@",array);
for(id obj in array)
NSString *urlStr=[obj objectForKey:@"img"];
【讨论】:
@Bhavani chada 现在您必须将 urlsrt 转换为 NSURL 对象,然后您可以继续前进.... 干杯以上是关于如何显示来自 JSON 的图像的水平滚动视图的主要内容,如果未能解决你的问题,请参考以下文章