这是我的 JSON 响应,
{
"AppConfig": {
"store_logo": "url",
"deal_status": "A",
"see_the_menu_btn": "A",
"store_id": "3",
"store_name": " Pizza Restaurant",
"bg_image": "www.my image.png"
}
}
NSString *localwthr = [NSString stringWithFormat:@"my url"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:localwthr]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
if (responsedata) {
NSDictionary *Dictionarydetails = [NSJSONSerialization
JSONObjectWithData:responsedata
options:kNilOptions
error:nil];
NSLog(@"The return data is: %@",Dictionarydetails);
NSString *imgURL=[[Dictionarydetails objectForKey:@"AppConfig"]objectForKey:@"bg_image"];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,100,100)];
imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: imgURL]]];
}
}
我需要获取键bg_image
的URL值,下载图像并将其设置在UIImageView
中。我怎样才能做到这一点?
最佳答案
imageUrl 永远不会像“www.my image.png”, imageUrl 就像“http://www.serverName.com/directory/..../imageName.png”
如果您的网址中有空格,那么您必须将其转换为 UTF8 格式,这是 webURL 的标准格式。 所以你应该使用,
imgURL = [imgURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//Use it as it shown in below code.
NSString *imgURL=[[jsonDict objectForKey:@"AppConfig"]objectForKey:@"bg_image"];
imgURL = [imgURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: imgURL]]];
干杯!
关于ios - 解析json数据图像到 ImageView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21473661/