且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将图像从 URL iphone sdk 加载到 tableView

更新时间:2023-11-10 22:29:22

你可以使用 GCD 在后台线程中加载图片,像这样:

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];
        });
    });

嗨.但是你可能需要添加一个 dispatch_release(concurrentQueue);确保没有泄漏.– Franck 8 月 25 日 19:43

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