Eric Partyka
Eric Partyka

Reputation: 43

JSON Image Parsing/Prefixed URL

I have established successful parsing of text and images in a table view using JSON/PHP/MYSQL. I am only storing the location of the images in the database and the actual images are stored in a directory on my server. The only thing that is stored related to the image in the database is the name. Example car.jpg. What I want to do is prefix the URL of the image location on my server so they can be parsed without me having to go into the DB and manually entering the URL. Here is some of my code...

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *identifier = @"studentsCell";

    StudentsCell *cell = (StudentsCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StudentsCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    NSDictionary *studentsDict = [students objectAtIndex:indexPath.row];
    //I want to prefix the URL for the key imagepath but i dont know where and how to do it.
    NSURL *imageURL = [NSURL URLWithString:[studentsDict objectForKey:@"imagepath"]];
    NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
    UIImage *imageLoad = [[UIImage alloc] initWithData:imageData];

    cell.imageView.image = imageLoad;

    NSString *name = [NSString stringWithFormat:@"%@ %@", [studentsDict valueForKey:@"first"], [studentsDict valueForKey:@"last"]];

    cell.title.text = name;

    NSString *subtitle = [NSString stringWithFormat:@"%@", [studentsDict objectForKey:@"email"]];

    cell.subtitle.text = subtitle;

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(265, 6, 44, 44);
    [button setImage:[UIImage imageNamed:@"email.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(email:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:button];

   // cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellbackground.png"]];

    return cell;
}

Upvotes: 4

Views: 647

Answers (1)

Rob
Rob

Reputation: 437622

Let's assume you had something like:

NSURL *baseURL = [NSURL URLWithString:@"http://www.your.site.here/images"]; // whatever the folder with the images is

Then you could do:

NSURL *imageURL = [baseURL URLByAppendingPathComponent:[studentsDict objectForKey:@"imagepath"]];

By the way, you should consider using a UIImageView category, such as SDWebImage. Then, instead of loading a NSData with the image data synchronously, you can do an asynchronous image load:

[cell.imageView setImageWithURL:imageURL
               placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

The placeholder is what should be shown while the image is being loaded (perhaps just a blank image), and SDWebImage will then asynchronously retrieve the image and update the cell when it's retrieved. This will yield a far more responsive user interface. It will also avail yourself of image caching (so if you scroll down and then back up, the image won't be retrieved again).

AFNetworking has a similar UIImageView category, but not quite as robust of an implementation. But if you're already using AFNetworking, it's an option.

Upvotes: 2

Related Questions