Genevios Genevios - 1 year ago 78
iOS Question

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!

Answer Source

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!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download