ios - 解析json数据图像到 ImageView

标签 ios objective-c json

这是我的 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/

相关文章:

objective-c - iOS 5 和应用商店

java - Android解析Json错误

ios - 调用 UITableView scrollEnabled 问题

objective-c - NSCell 上的动画

objective-c - 缓存自定义类

json - 如何从 R 编写一个具有经纬度序列的 json 数组?

javascript - 尝试使用转义的单引号解析JSON时Javascript中的“Unexpected identifier”错误

ios - 从带有披露指示器的 TableView 单元格 xib 转到 Storyboard

ios - 如何使用 MPVolumeView 来更改音频输出设备。喜欢 WhatsApp 和视频群聊

ios - 从后端服务器提取内容更新的最佳解决方案?