Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift Grand Central调度队列和图像_Ios_Swift_Uiimageview_Grand Central Dispatch - Fatal编程技术网

Ios Swift Grand Central调度队列和图像

Ios Swift Grand Central调度队列和图像,ios,swift,uiimageview,grand-central-dispatch,Ios,Swift,Uiimageview,Grand Central Dispatch,我知道这类问题已经被问了7次,但我遇到了一个具体问题,我认为这个问题没有涵盖/非常明显,但我太新手了,无法自己解决 在TableViewController中的cellForRowAt方法中有以下代码段: 新闻是我的数据结构。该代码也可以正常执行,但是根据控制台输出,每个缩略图都是0x0大小的图像,大小为{0,0}0,比例为1.000000 有人知道如何下载这些图像,但不立即将它们分配到UIImageView,而是将它们存储起来供以后使用吗?问题是,在全局调度队列开始处理url之前,您就创建了新

我知道这类问题已经被问了7次,但我遇到了一个具体问题,我认为这个问题没有涵盖/非常明显,但我太新手了,无法自己解决

在TableViewController中的cellForRowAt方法中有以下代码段:

新闻是我的数据结构。该代码也可以正常执行,但是根据控制台输出,每个缩略图都是0x0大小的图像,大小为{0,0}0,比例为1.000000


有人知道如何下载这些图像,但不立即将它们分配到UIImageView,而是将它们存储起来供以后使用吗?

问题是,在全局调度队列开始处理url之前,您就创建了新闻文章。因此,缩略图仍然是在第一行中创建的空UIImage

您必须在内部分派闭包内创建缩略图,如:

for item in parser.parsedData {
    guard let currentDictionary = item as? Dictionary<String,String>    else { continue /* or some error handling */ }
    guard let title = currentDictionary["title"]                        else { continue /* or some error handling */ }
    guard let link = currentDictionary["link"]                          else { continue /* or some error handling */ }
    guard let urlString = currentDictionary["media:content"]            else { continue /* or some error handling */ }
    guard let url = URL(string: urlString)                              else { continue /* or some error handling */ }

    DispatchQueue.global().async {
        if let data = try? Data(contentsOf: url) {
            DispatchQueue.main.sync {
                if let thumbnail = UIImage(data: data) {
                    let newsArticle = News(title: title, link: link, thumbnail: thumbnail)

                    news.append(newsArticle)
                }
            }

        }
    }
}

相反,引用IndexPath,检索clousure中的单元格,然后继续使用该单元格。但是正如您已经提到的,关于这个问题,stackoverflow上有很多条目。

这是一个相当广泛的问题-我建议您使用现有的库来处理图像,而不是直接回答。它们通常有一种基于URL异步设置图像的方法,也有一种预取图像的方法,而无需手动存储图像。我使用翠鸟,但另一个流行的是SDWebImage。如果出于某种原因要求您不使用第三方库,请结合我使用的URLSession查看URLCache,否则您必须实现自己的缓存。无论如何,除非真的有必要,否则我会尝试使用翠鸟或SDWebImage库Library@Moritz你说得对,那只是从上面复制粘贴而已。还有很多地方需要改进,但我只关注了空缩略图和非零缩略图的一个问题。但我只是改进了它。。。
var thumbnail = UIImage()

for item in parser.parsedData {
    let currentDictionary = item as Dictionary<String,String>
    let title = currentDictionary["title"]
    let link = currentDictionary["link"]
    let urlString = currentDictionary["media:content"]
    let url = NSURL(string: urlString!)


    if urlString != nil {
        let url = NSURL(string: urlString!)
        DispatchQueue.global().async {
            let data = try? Data(contentsOf: url! as URL)
            DispatchQueue.main.sync {
                thumbnail = UIImage(data: data!)!
            }
        }
    }

    var newsArticle: News!
    newsArticle = News(title: title!, link: link!, thumbnail: thumbnail)

    news.append(newsArticle)
for item in parser.parsedData {
    guard let currentDictionary = item as? Dictionary<String,String>    else { continue /* or some error handling */ }
    guard let title = currentDictionary["title"]                        else { continue /* or some error handling */ }
    guard let link = currentDictionary["link"]                          else { continue /* or some error handling */ }
    guard let urlString = currentDictionary["media:content"]            else { continue /* or some error handling */ }
    guard let url = URL(string: urlString)                              else { continue /* or some error handling */ }

    DispatchQueue.global().async {
        if let data = try? Data(contentsOf: url) {
            DispatchQueue.main.sync {
                if let thumbnail = UIImage(data: data) {
                    let newsArticle = News(title: title, link: link, thumbnail: thumbnail)

                    news.append(newsArticle)
                }
            }

        }
    }
}
DispatchQueue.main.async {
    // Never do this
    cell.thumbnailImageView.image = UIImage(data: data!)
}