将图像从 URL iphone sdk 加载到 tableView [英] Load image to a tableView from URL iphone sdk

查看:35
本文介绍了将图像从 URL iphone sdk 加载到 tableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 tableView,需要从 URL 加载图像.我有一个包含图像 URL 的数组,当页面加载时,我需要将所有图像加载到 tableview 中.请注意,不是来自单个 URL,而是具有不同 URL 的数组.我该如何实施?请帮忙

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. Note that, not from a single URL, have an array with different URLs. How can I implement that? Please help

谢谢.

推荐答案

你可以使用 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

这篇关于将图像从 URL iphone sdk 加载到 tableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆