Question

I was wondering if it is possible in general to load images via an URL which is parsed via JSON into an UIImageView in a tableview.

I tried this with no success in the UI and only two images in the log. I don't understand why he is showing me only 2 urls instead of 18.

   NSString *bildurl = [[arrayArtikelBild objectAtIndex:indexPath.row] objectForKey:@"bild"];
   NSLog(@"BildURL: %@", bildurl);
   UIImage *image = [UIImage imageWithContentsOfFile:bildurl];
   cell.artikelImage.image = image;

I know that my parsing code work because I let it parse and display other things already (I use NSJSONSerialization).

Thanks in advance.

Was it helpful?

Solution

You could theoretically use:

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:bildurl]]];

but it's highly inadvisable since all the downloads will be done on the main thread + there's no caching. You need to load images asynchronous to achieve smooth scrolling. I'd recommend using https://github.com/rs/SDWebImage or AFNetworking's UIImageView extension (https://github.com/AFNetworking/AFNetworking)

OTHER TIPS

You can Using GCD (Grand Central Dispatch) for load image asynchronous

Using this will increase the performance of loading tableview definitely.

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

    dispatch_async(queue, ^{
       UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:bildurl]]];

        dispatch_sync(dispatch_get_main_queue(), ^{
            [cell.artikelImage setImage:image];
            [cell setNeedsLayout];
        });
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top