Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How make parse image from JSON

I have some project where i must paste image from JSON. I try to find any tutorial about this, try to see any video from this theme but nothing. So my problem have:

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

I added text 1 to cell also text 2 its work perfect but how to add image to to array 3(image in cell in my tableView)? I tried also to added for image but its not work for me:

NSURL *imageURL = [NSURL URLWithString:[appsdict objectForKey:@"image"]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *imageLoad = [[UIImage alloc] initWithData:imageData];
cell.imageView.image = imageLoad;

Please help with my problem or maybe give some tutorial with parse image from JSON, also youtube haven't perfect tutorial from JSON parse. Thanks!

like image 633
Genevios Avatar asked Oct 22 '13 06:10

Genevios


1 Answers

Don't keep multiple data source like array1, array2, array3 etc. It is not good coding and will confuse while debugging/fixing issues. Instead maintain single array with information for displaying in individual cell of the table view.

for (NSDictionary *dict in playlist) {
    // array is single data source
    [array addObject:diction];
}

Then while assigning data to the table view cell use,

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

Like I mentioned in comments for lazy loading images from the URLs in JSON response, use SDWebImage. Usage is simple like I have shown above. Alternately you can implement lazy loading yourself by studying this sample code from Apple: LazyTableImages

Hope that helps!

like image 139
Amar Avatar answered Oct 15 '22 02:10

Amar