Load image to a tableView from URL iphone sdk

前端 未结 6 1449
攒了一身酷
攒了一身酷 2020-12-02 17:53

I have tableView and need to load image from URL. I have an array that contains the URLs of images and when the page loads I need to load all the images into the tableview.

6条回答
  •  不思量自难忘°
    2020-12-02 18:23

    You can use GCD to load images in background thread, like this:

    //get a dispatch queue
    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        //this will start the image loading in bg
        dispatch_async(concurrentQueue, ^{        
            NSData *image = [[NSData alloc] initWithContentsOfURL:imageURL];
    
            //this will set the image when loading is finished
            dispatch_async(dispatch_get_main_queue(), ^{
                imageView.image = [UIImage imageWithData:image];
            });
        });
    

    Hi. But you probably need to add a dispatch_release(concurrentQueue); to be sure no leak. – Franck Aug 25 at 19:43

提交回复
热议问题