user3251949
user3251949

Reputation: 41

Parse json data image to image view

This is my JSON response,

 {
        "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]]];
    }
}

I need to get URL value for key bg_image, download image and set it in the UIImageView. How can I do this?

Upvotes: 2

Views: 1696

Answers (4)

Hitendra Solanki
Hitendra Solanki

Reputation: 4941

imageUrl is never like "www.my image.png", imageUrl is like "http://www.serverName.com/directory/..../imageName.png"

if there is space in your url then you have to convert it into UTF8 format, which is a standard format for webURL. so You should use,

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]]];

cheers!

Upvotes: 2

Rajjjjjj
Rajjjjjj

Reputation: 434

check out this code , is this you are needed or not as rmaddy told post your done code as far as now

NSString *imageName=[[jsonDict objectForKey:@"AppConfig"]objectForKey:@"bg_image"];
imageview.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: imageName]]];

Upvotes: 0

Rajesh Loganathan
Rajesh Loganathan

Reputation: 11217

Try This:

NSString *imgURL=[[jsonDict objectForKey:@"AppConfig"]objectForKey:@"bg_image"];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,100,100)];
imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: imgURL]]];

Upvotes: 0

Kumar KL
Kumar KL

Reputation: 15335

For loading the image from the URL, then

NSString *imgURL=[[jsonDict objectForKey:@"AppConfig"]objectForKey:@"bg_image"];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: imgURL]]];

Upvotes: 1

Related Questions