0

I'm using this code to load an image into my UITableView.

But nothing is loading, any ideas why? The link is correct when I NSLog it.

NSString *temp = [appointmentDictionaryTemp objectForKey:@"patient_small_photo_url"]; 
NSData * imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:temp]];
UIImage * image = [UIImage imageWithData:imageData];
UIImageView * myImageView = [[UIImageView alloc] initWithImage:image];
cell.patientImage = myImageView; 

Update:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"appointmentCell";

    AppointmentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    NSDictionary *appointmentDictionaryTemp = [self.appointmentArray objectAtIndex:indexPath.row];
    cell.patientNameLabel.text = [appointmentDictionaryTemp objectForKey:@"patient"];
    cell.appointmentTimeLabel.text = [appointmentDictionaryTemp objectForKey:@"scheduled_time"];

    NSString *urlString = [[appointmentDictionaryTemp objectForKey:@"patient_small_photo_url"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSData * imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
    UIImage * image = [UIImage imageWithData:imageData];
    cell.patientImage.image = image; 

    return cell;
}
Jon
  • 4,732
  • 6
  • 44
  • 67

2 Answers2

1

Have you tried with:

cell.patientImage.image = image;

Of course you don't need to create myImageView.

dandan78
  • 13,328
  • 13
  • 64
  • 78
  • 1
    Sorry I give you the answer without any explaination: the error you made is to assign to the imageview of the cell another imageview, instead of just set the image of the already existing view. – il Malvagio Dottor Prosciutto Nov 16 '11 at 23:24
  • This worked. But now its choppy when scrolling through the table. I'm posting the full code in the question if you can find any way to optimize scrolling. Thanks. – Jon Nov 16 '11 at 23:24
  • Well, that's because you're using dataWithContentsOfURL in a table view. You'd be better off asking that separately (or just looking it up yourself) because it's a much bigger issue. – Steven Fisher Nov 16 '11 at 23:26
  • you need to download images async. there're at least 3-4 ways to do that, from using libreries like AFNetworking to use NSQueue... – il Malvagio Dottor Prosciutto Nov 16 '11 at 23:28
  • Added question here: http://stackoverflow.com/questions/8160223/how-to-make-images-in-uitableviewcell-scroll-smoothly – Jon Nov 16 '11 at 23:31
0

Try adding percent escapes to your URL string.

NSString *urlString = [[appointmentDictionaryTemp objectForKey:@"patient_small_photo_url"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Mark Adams
  • 30,776
  • 11
  • 77
  • 77
  • No luck unfortunately. The link is `https` instead of `http` if that makes a difference. – Jon Nov 16 '11 at 23:17
  • Set a breakpoint after you send the `-dataWithContentsOfURL` message and check to see what's coming over the wire. Maybe it's a problem with setting the image on the image view. How are you setting up your cell? Is `patientImage` connected to a UIImageView? – Mark Adams Nov 16 '11 at 23:23