Swift 如何从另一个线程提供图像?

Swift 如何从另一个线程提供图像?,swift,multithreading,Swift,Multithreading,我有一个获取用户图片的代码: if let photoURL = message[Constants.MessageFields.photoURL], let URL = URL(string: photoURL), let data = try? Data(contentsOf: URL) { cell.userPic.image = UIImage(data: data) } 当我使用它时,tableView在滚动方面落后了 请帮助我将此代码放到另一个线程中。这是苹果提供的

我有一个获取用户图片的代码:

if let photoURL = message[Constants.MessageFields.photoURL], let URL = URL(string: photoURL),
    let data = try? Data(contentsOf: URL) {
    cell.userPic.image = UIImage(data: data)
}
当我使用它时,tableView在滚动方面落后了


请帮助我将此代码放到另一个线程中。

这是苹果提供的一个很好的示例,您可以根据自己的需要进行调整:

基本思想是为图像创建AsyncFetcher,并将图像创建代码置于单独的操作中

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Cell.reuseIdentifier, for: indexPath) as? Cell else {
        fatalError("Expected `\(Cell.self)` type for reuseIdentifier \(Cell.reuseIdentifier). Check the configuration in Main.storyboard.")
    }

    let model = models[indexPath.row]
    let id = model.id
    cell.representedId = id

    // Check if the `asyncFetcher` has already fetched data for the specified identifier.
    if let fetchedData = asyncFetcher.fetchedData(for: id) {
        // The data has already been fetched and cached; use it to configure the cell.
        cell.configure(with: fetchedData)
    } else {
        // There is no data available; clear the cell until we've fetched data.
        cell.configure(with: nil)

        // Ask the `asyncFetcher` to fetch data for the specified identifier.
        asyncFetcher.fetchAsync(id) { fetchedData in
            DispatchQueue.main.async {
                /*
                 The `asyncFetcher` has fetched data for the identifier. Before
                 updating the cell, check if it has been recycled by the
                 collection view to represent other data.
                 */
                guard cell.representedId == id else { return }

                // Configure the cell with the fetched image.
                cell.configure(with: fetchedData)
            }
        }
    }

    return cell
}
但在你的情况下,你应该使用


我可以确认,这种方法是有效的,如果正确的话,结果会是平滑的滚动和良好的用户体验。这是苹果公司提供的一个很好的示例,您可以根据自己的需要进行调整:

基本思想是为图像创建AsyncFetcher,并将图像创建代码置于单独的操作中

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Cell.reuseIdentifier, for: indexPath) as? Cell else {
        fatalError("Expected `\(Cell.self)` type for reuseIdentifier \(Cell.reuseIdentifier). Check the configuration in Main.storyboard.")
    }

    let model = models[indexPath.row]
    let id = model.id
    cell.representedId = id

    // Check if the `asyncFetcher` has already fetched data for the specified identifier.
    if let fetchedData = asyncFetcher.fetchedData(for: id) {
        // The data has already been fetched and cached; use it to configure the cell.
        cell.configure(with: fetchedData)
    } else {
        // There is no data available; clear the cell until we've fetched data.
        cell.configure(with: nil)

        // Ask the `asyncFetcher` to fetch data for the specified identifier.
        asyncFetcher.fetchAsync(id) { fetchedData in
            DispatchQueue.main.async {
                /*
                 The `asyncFetcher` has fetched data for the identifier. Before
                 updating the cell, check if it has been recycled by the
                 collection view to represent other data.
                 */
                guard cell.representedId == id else { return }

                // Configure the cell with the fetched image.
                cell.configure(with: fetchedData)
            }
        }
    }

    return cell
}
但在你的情况下,你应该使用

我可以确认这种方法是有效的,当正确的操作时,结果是平滑的滚动和良好的用户体验